470,850 Members | 1,029 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,850 developers. It's quick & easy.

DataGridView - Not much doc out there

20 16bit
Can anyone tell me how I can tell if a checkbox in a DataGridView (I call it DGV1) is checked? When I click one box, I get to the DGV1_CellClick event. From there, I am lost. A lot of the examples show a foreach, but I am not processing ALL of the rows. I just want to look at the ONE cell in ONE column and ONE row, and see if it is the one that was clicked/checked. In other words, the click of WHICH checkboxbox caused this event to trigger?
Any help would be greatly appreciated!
Dave
May 4 '21 #1

✓ answered by SioSio

Use the DataGridView.CellValueChanged event to know that the checkbox is checked (or unchecked). However, the CellValueChanged event is fired when the value is committed, such as by moving the focus to another cell after the checkbox is checked. To have the CellValueChanged event fire immediately after the checkbox is checked, call the DataGridView.CommitEdit method in the CurrentCellDirtyStateChanged event handler to commit the value.
Expand|Select|Wrap|Line Numbers
  1.         //CurrentCellDirtyStateChanged Event Handler
  2.         private void DataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
  3.         {
  4.             if (dataGridView1.CurrentCellAddress.X == 0 && dataGridView1.IsCurrentCellDirty)
  5.             {
  6.                 //Commit
  7.                 dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
  8.             }
  9.         }
  10.  
  11.         //CellValueChanged Event Handler
  12.         private void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  13.         {
  14.                 MessageBox.Show(
  15.                     string.Format("The value of the checkbox in the row[{0}] has changed to {1}.",
  16.                               e.RowIndex,dataGridView1[e.ColumnIndex, e.RowIndex].Value));
  17.         }

1 1873
SioSio
270 256MB
Use the DataGridView.CellValueChanged event to know that the checkbox is checked (or unchecked). However, the CellValueChanged event is fired when the value is committed, such as by moving the focus to another cell after the checkbox is checked. To have the CellValueChanged event fire immediately after the checkbox is checked, call the DataGridView.CommitEdit method in the CurrentCellDirtyStateChanged event handler to commit the value.
Expand|Select|Wrap|Line Numbers
  1.         //CurrentCellDirtyStateChanged Event Handler
  2.         private void DataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
  3.         {
  4.             if (dataGridView1.CurrentCellAddress.X == 0 && dataGridView1.IsCurrentCellDirty)
  5.             {
  6.                 //Commit
  7.                 dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
  8.             }
  9.         }
  10.  
  11.         //CellValueChanged Event Handler
  12.         private void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  13.         {
  14.                 MessageBox.Show(
  15.                     string.Format("The value of the checkbox in the row[{0}] has changed to {1}.",
  16.                               e.RowIndex,dataGridView1[e.ColumnIndex, e.RowIndex].Value));
  17.         }
May 6 '21 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Mitchell S. Honnert | last post: by
7 posts views Thread by =?Utf-8?B?TG9zdEluTUQ=?= | last post: by
4 posts views Thread by =?Utf-8?B?anAybXNmdA==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.