I have a DataGridView which has a bunch of columns plus a checkBoxColumn called IsChecked.
I want to raise an event whenever a row's checked state changes. Checking or Unchecking a row may be done either when user clicks the checkBox or when they press space key on a row.
this is how I added checkBoxColumn to my grid:
dgvMain.Columns.Add(new DataGridViewCheckBoxColumn {
Name = "IsChecked" ,
Width = 20,
Visible = false,
HeaderText = "",
SortMode = DataGridViewColumnSortMode.NotSortable,
DisplayIndex = Columns.Count, //to be displayed as last column
ValueType = typeof(bool),
FalseValue = false,
TrueValue = true
});
and this is how I check the cell when space key is pressed:
private void dgvMain_KeyDown(object sender, KeyEventArgs e)
{
foreach (DataGridViewRow row in dgvMain.SelectedRows)
{
bool? checkState = (bool?)row.Cells["IsChecked"].Value;
if (checkState == null || checkState == false)
checkState = true;
else
checkState = false;
row.Cells["IsChecked"].Value = checkState;
}
}
Attempt # 1:
I tried using CellEndEdit event which only helped when you check the cell using mouse, but when I press space and the cell checks/unchecks CellEndEdit does not fire.
Attempt # 2:
I tried using CellValueChanged event, which works fine when I press space, and also when I check the box using mouse and leave the row but when I check and uncheck the box multiple times, nothing happens. It seems like CellValueChanged raises when the cell is finished editing by mouse.
Attempt # 3:
I Also tried using CurrentCellDirtyStateChanged which responds to first checked change caused by mouse but does not respond to rapid checked changes bu mouse, only the first one and also when you leave the row. I want to catch all checked changes, even if user changes the checked state rapidly by clicking on the checkBox, that counts too.
I'm not sure if there is any event already for this purpose or not. If not, how can I add an eventHandler for this column programmatically?
DataTable(orDataSet), then put the event on itsColumnChangedorColumnChangingevent. - DonBoitnottDataBound, when you create the cell, add a handler to it. - user959631