9

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?

davmos
  • 9,324
  • 4
  • 40
  • 43
user2516034
  • 155
  • 3
  • 3
  • 8
  • 1
    This is a bad title. Please update it. You can read http://meta.stackexchange.com/questions/10647/how-do-i-write-a-good-title – Soner Gönül Jun 24 '13 at 12:16
  • The link below answers your question: http://social.msdn.microsoft.com/Forums/windows/en-US/31c7a954-1a85-4c38-9e9f-e157d33faf0b/how-to-get-the-value-of-checkbox-in-the-datagridview – Colm McKenna Jun 24 '13 at 13:05
  • Has your question been answered? Or are you still having problems? – Derek W Jun 24 '13 at 21:34

4 Answers4

22

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.

Derek W
  • 9,708
  • 5
  • 58
  • 67
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

Community
  • 1
  • 1
Arie
  • 5,251
  • 2
  • 33
  • 54
1

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));
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
Rtisatto
  • 767
  • 8
  • 7