Furqan Sehgal
Furqan Sehgal

Reputation: 4997

Datagridview checkbox column's value

I have a DataGridView with a checkbox column. I want to capture the value of the checkbox immediately when user changes it by clicking. I tried several events (CellValueChanged, CellClicked, CurrentCellDirtyStateChanged etc.) but nothing worked.

This is my code:

If dgvIDsTBC.CurrentRow.Cells(2).Value = True Then
    MsgBox("True")
End If

Please help

Upvotes: 1

Views: 4862

Answers (1)

V4Vendetta
V4Vendetta

Reputation: 38230

Hope this helps

void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
     if (e.ColumnIndex == 3)
        MessageBox.Show(dataGridView1[e.ColumnIndex, e.RowIndex].FormattedValue.ToString());
}

This i presume you would have done, now the catch is unless you move out of the cell the grid considers you are still editing ,So Add this part

void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.ColumnIndex == 3)
       dataGridView1.EndEdit();
}

This should be fine for you, 3 is the checkbox column you intend it to work for

Upvotes: 4

Related Questions