DataGridView - Not much doc out there

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DGrund
    New Member
    • Jan 2017
    • 23

    DataGridView - Not much doc out there

    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
  • SioSio
    Contributor
    • Dec 2019
    • 272

    #2
    Use the DataGridView.Ce llValueChanged event to know that the checkbox is checked (or unchecked). However, the CellValueChange d 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 CellValueChange d event fire immediately after the checkbox is checked, call the DataGridView.Co mmitEdit method in the CurrentCellDirt yStateChanged event handler to commit the value.
    Code:
    		//CurrentCellDirtyStateChanged Event Handler
    		private void DataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    		{
    			if (dataGridView1.CurrentCellAddress.X == 0 && dataGridView1.IsCurrentCellDirty)
    			{
    				//Commit
    				dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
    			}
    		}
    
    		//CellValueChanged Event Handler
    		private void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    		{
    				MessageBox.Show(
    					string.Format("The value of the checkbox in the row[{0}] has changed to {1}.",
    				              e.RowIndex,dataGridView1[e.ColumnIndex, e.RowIndex].Value));
    		}

    Comment

    Working...