Checkboxes in DataGridView not firing CellValueChanged event
Asked Answered
S

5

11

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?

Shien answered 24/6, 2013 at 12:11 Comment(3)
This is a bad title. Please update it. You can read meta.stackexchange.com/questions/10647/…Misbecome
The link below answers your question: social.msdn.microsoft.com/Forums/windows/en-US/…Browse
Has your question been answered? Or are you still having problems?Astigmatism
A
24

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.

Astigmatism answered 24/6, 2013 at 13:51 Comment(0)
L
5

MSDN says here that CellValueChanged won't fire until the cell has lost focus.

Some solutions:

DataGridView.CellContentClick

http://codingeverything.blogspot.com/2013/01/firing-datagridview-cellvaluechanged.html

Liberticide answered 24/6, 2013 at 12:59 Comment(0)
B
3

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));
Bedstead answered 16/2, 2015 at 15:12 Comment(0)
A
0

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
Almost answered 13/12, 2013 at 16:20 Comment(0)
A
0

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));
Anvil answered 10/5 at 20:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.