How to Detect Datagridview Checkbox Event Change

How to detect DataGridView CheckBox event change?

To handle the DatGridViews CheckedChanged event you must first get the CellContentClick to fire (which does not have the CheckBoxes current state!) then call CommitEdit. This will in turn fire the CellValueChanged event which you can use to do your work. This is an oversight by Microsoft. Do some thing like the following...

private void dataGridViewSites_CellContentClick(object sender, 
DataGridViewCellEventArgs e)
{
dataGridViewSites.CommitEdit(DataGridViewDataErrorContexts.Commit);
}

/// <summary>
/// Works with the above.
/// </summary>
private void dataGridViewSites_CellValueChanged(object sender,
DataGridViewCellEventArgs e)
{
UpdateDataGridViewSite();
}

I hope this helps.

P.S. Check this article https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcelldirtystatechanged(v=vs.110).aspx

C# DataGridView Checkbox checked event

You can handle CellContentClick event of your DataGridView and put the logic for changing those cells there.

The key point is using CommitEdit(DataGridViewDataErrorContexts.Commit) to commits changes in the current cell to the data cache without ending edit mode. This way when you check for value of cell in this event, it returns current checked or unchecked value which you see in the cell currently after click:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//We make DataGridCheckBoxColumn commit changes with single click
//use index of logout column
if(e.ColumnIndex == 4 && e.RowIndex>=0)
this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);

//Check the value of cell
if((bool)this.dataGridView1.CurrentCell.Value == true)
{
//Use index of TimeOut column
this.dataGridView1.Rows[e.RowIndex].Cells[3].Value = DateTime.Now;

//Set other columns values
}
else
{
//Use index of TimeOut column
this.dataGridView1.Rows[e.RowIndex].Cells[3].Value = DBNull.Value;

//Set other columns values
}
}

Triggering a checkbox value changed event in DataGridView

A colleague of mine recommends trapping the CurrentCellDirtyStateChanged event. See http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcelldirtystatechanged.aspx.

DataGridView event for checked change

In the CurrentCellDirtyStateChanged event handler, trigger EndEdit so that the value changed event is triggered.

void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty && dataGridView1.CurrentCell.ColumnIndex == CheckColumnIndex)
{
dataGridView1.EndEdit();
}
}

In the CellValueChanged event handler get the value from the CheckBoxColumn.

void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == CheckColumnIndex)
{
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
bool achecked = Convert.ToBoolean(checkCell.Value);
}
}

Datagridview checkbox checked when clicking the cell

As Bioukh points out, you must call NotifyCurrentCellDirty(true) to trigger your event handler. However, adding that line will no longer update your checked state. To finalize your checked state change on click we'll add a call to RefreshEdit. This will work to toggle your cell checked state when the cell is clicked, but it will also make the first click of the actual checkbox a bit buggy. So we add the CellContentClick event handler as shown below and you should be good to go.


private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewCheckBoxCell cell = this.dataGridView1.CurrentCell as DataGridViewCheckBoxCell;

if (cell != null && !cell.ReadOnly)
{
cell.Value = cell.Value == null || !((bool)cell.Value);
this.dataGridView1.RefreshEdit();
this.dataGridView1.NotifyCurrentCellDirty(true);
}
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
this.dataGridView1.RefreshEdit();
}

DataGridView CheckBox, revert the Value after user confirmation

You can use such criteria:

Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, _
ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
If (e.ColumnIndex = 0 AndAlso e.RowIndex >= 0) Then
Dim value = DirectCast(DataGridView1(e.ColumnIndex, e.RowIndex).FormattedValue, _
Nullable(Of Boolean))
Dim result = MessageBox.Show("Are you sure to change vaule?", "", _
MessageBoxButtons.YesNoCancel)
If (result = System.Windows.Forms.DialogResult.Yes) Then
If (value.HasValue AndAlso value = True) Then
DataGridView1(e.ColumnIndex, e.RowIndex).Value = False
Else
DataGridView1(e.ColumnIndex, e.RowIndex).Value = True
End If
End If
End If
End Sub

In above code I showed a confirm message to the user and the if the user choose Yes, I reverted the value of cell.

In the above code I used e.ColumnIndex = 0 to show the confirm for first column. You may need some other criteria, for example e.ColumnIndex = 1. Or as another example (e.ColumnIndex >=1 AndAlso e.ColumnIndex <=13).

e.RowIndex >= 0 Makes sure the event is handled for a data cell and not a header cell.

dataGridView CheckBox checked dosen't work

You could change your event to a CellContentClick event instead, and then check if it's your checkbox column:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn)
{
DataGridViewCheckBoxCell cbCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];

if (cbCell.Value == cbCell.TrueValue)
{
cbCell.Value = cbCell.FalseValue;
}
else
{
cbCell.Value = cbCell.TrueValue;
}
}
}


Related Topics



Leave a reply



Submit