views:

3782

answers:

3

Hi, i have a check box in a datagridview windows form and have a event handler cell_Click on cell click i check the datagridview column for a check box it shows true if the cell is selected too(that is the check box is unchecked and only the the datagrid view cell is selected) and the check box is not selected .i tried for the column gettype and found out the type it shows DatagridViewCheckBox but wrong checked values .???

A: 

Several things here:

  • the cell click event just means that the user clicked with the mouse button on the data grid view, what you're looking for is probably the CellValueChanged
  • this event will give you the coordinates of the cell that changed. You should check to see if it's in your check box column, then get a reference to the cell and you can check the cell.Value to see if it's true or false. You're not going to find any values on the DataGridViewCheckBoxColumn -- it's going to be at the cell level, and you'll always find the value stored in cell.Value, no matter what type of column it is.
Clyde
+4  A: 

If I understand you correctly you are saying the checkbox value does not align with the underlying data?

This may well be because the data has been updated and is 'dirty', e.g. it hasn't been committed to the datasource yet. If you add an event handler like this:

private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
   if (dataGridView1.CurrentCell is System.Windows.Forms.DataGridViewCheckBoxCell)
   {
      dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
   }
}

Then that should update the datasource and you'll have the correct checkbox state when you query the cell.

Ian
+1  A: 

Hi , I found an answer for my question .This link was the one which helpted me get a solution. Also both answers from Clyde and Ian were also a part of it Thank you. Here is the link. http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcelldirtystatechanged.aspx