locked
how to get value returned from ThreeState Property? RRS feed

  • Question

  • In DataGridView I have 1 DataGridViewCheckBoxColumn,set ThreeState=true. I want when tick into CheckBox,for one status it will export an any value in TextBox

    private void grd_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                if ((grd.CurrentCell is DataGridViewCheckBoxCell) && (!grd.CurrentCell.Value.Equals(CheckState.Checked)))
                {
                         textBox1.Text = "true value";
                }
                if ((grd.CurrentCell is DataGridViewCheckBoxCell) && (!grd.CurrentCell.Value.Equals(CheckState.Unchecked)))
                {
                        textBox1.Text = "fasle value";
                }
                if ((grd.CurrentCell is DataGridViewCheckBoxCell) && (!grd.CurrentCell.Value.Equals(CheckState.Indeterminate)))
                {
                  
                    textBox1.Text = "indeterminate value";
                }
            }

    when I debug program, its only export the last value "indeterminate value".please show me how to set status Check,Uncheck,Indeterminate is right?

     


    binhnt
    Wednesday, March 24, 2010 10:19 AM

Answers

  • You have a bug in your code:

     

    if ((grd.CurrentCell is DataGridViewCheckBoxCell) && (!grd.CurrentCell.Value.Equals(CheckState.Indeterminate)))

    This is going to set the text to "indeterminate value" any time the current cell value does not equal CheckState.Indeterminate.  I think you need to remove all three ! in your second condition.

    However, I would also recommend rewriting this routine as:

    private void grd_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        DataGridViewCheckBoxCell box = grd.CurrentCell as DataGridViewCheckBoxCell;
    
        if (box != null)
        {
              CheckState value = (CheckState)box.Value;
              switch(value)
              {
                  case CheckState.Checked:
                      textBox1.Text = "true value";
                      break;
                  case CheckState.Unchecked:
                      textBox1.Text = "false value";
                      break;
                  default:
                      textBox1.Text = "indeterminate value";
                      break;
              }
        }
    }

    This, in my opinion, is less error prone and easier to follow.


    Reed Copsey, Jr. - http://reedcopsey.com
    • Proposed as answer by CiaranODonnell Wednesday, March 24, 2010 5:43 PM
    • Marked as answer by Figo Fei Monday, March 29, 2010 6:50 AM
    Wednesday, March 24, 2010 5:20 PM
    Moderator