Reputation: 175
I'm using this code:
// Cell value change event.
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if ((bool)dataGridView1.CurrentCell.Value == true) MessageBox.Show("true");
if ((bool)dataGridView1.CurrentCell.Value == false) MessageBox.Show("false");
MessageBox.Show(dataGridView1.CurrentCell.Value.ToString());
}
It works fine for all columns, except for one column with a checkbox (DataGridViewCheckBoxColumn
)
I need to know the value in the checkbox column (true or false).
What do I need to do for this?
Upvotes: 11
Views: 16540
Reputation: 371
I liked @Andrew Phillips solution to use the CurrentCellDirtyStateChanged event as it seemed simple and one line of code. In my case I had several CheckBox columns that needed to be evaluated, so I made a slight modification to keep it one line and fire if a cell of any of the CheckBox columns were modified:
if (DGV_Charge.Columns[DGV_Charge.CurrentCell.ColumnIndex] is DataGridViewCheckBoxColumn) DGV_Charge_CellValueChanged(null, new DataGridViewCellEventArgs(DGV_Charge.CurrentCell.ColumnIndex, DGV_Charge.CurrentCell.RowIndex));
Upvotes: 0
Reputation: 41
I came up with a slightly different solution.
I use the CurrentCellDirtyStateChanged event to check if the column is the checkbox column, and if it is I manually fire the CellValueChanged event like so:
if (DGV_Charge.CurrentCell.ColumnIndex == 0) DGV_Charge_CellValueChanged(null, new DataGridViewCellEventArgs(DGV_Charge.CurrentCell.ColumnIndex, DGV_Charge.CurrentCell.RowIndex));
Upvotes: 3
Reputation: 785
The best is to extend the grid by creating your own grid, ready with these various "tricks". Believe me, there are a lot of things that need for adjustments in this grid.
Suggested code using
Public Class MyGrid
Inherits Windows.Forms.DataGridView
Private Sub MyGrid_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.CurrentCellDirtyStateChanged
If Me.IsCurrentCellDirty AndAlso Not Me.CurrentCell Is Nothing Then
If TypeOf Me.Columns(Me.CurrentCell.ColumnIndex) Is DataGridViewCheckBoxColumn Then
Me.CommitEdit(DataGridViewDataErrorContexts.Commit) 'imediate commit, not only on focus changed
End If
End If
End Sub
Private Sub MyGrid_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
Handles Me.CellValueChanged
If e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then
Dim Checked As Boolean = False
If TypeOf Me.Columns(e.ColumnIndex) Is DataGridViewCheckBoxColumn Then
'avoid erros
Checked = Me.CellChecked(e.RowIndex, e.ColumnIndex)
End If
RaiseEvent Change(e.RowIndex, e.ColumnIndex, Checked)
End If
End Sub
End Class
Upvotes: 0
Reputation: 10026
Working with DataGridViewCheckBoxColumn
can sometimes be a bit tricky since there are some rules that specifically apply only to the Cells
of this column type. This code should handle the issue that you are experiencing.
The CurrentCellDirtyStateChanged
event commits the changes immediately when the cell is clicked. You manually raise the CellValueChanged
event when calling the CommitEdit
method.
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.CurrentCell == null) return;
if ((bool)dataGridView1.CurrentCell.Value == true) MessageBox.Show("true");
if ((bool)dataGridView1.CurrentCell.Value == false) MessageBox.Show("false");
MessageBox.Show(dataGridView1.CurrentCell.Value.ToString());
}
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
Visit here for additional information on working with the DataGridViewCheckBoxCell
.
Upvotes: 25
Reputation: 5373
MSDN says here that CellValueChanged won't fire until the cell has lost focus.
Some solutions:
http://codingeverything.blogspot.com/2013/01/firing-datagridview-cellvaluechanged.html
Upvotes: 5