I want to check a check-box which is inside Gridview programmatic on click of a button
Asked
Active
Viewed 3,703 times
0
-
try to follow this thread http://stackoverflow.com/questions/1237829/datagridview-checkbox-column-value-and-functionality Regards. – Carmelo La Monica Sep 26 '11 at 15:46
-
You mean all of the checkBoxes on a button click? – Mitja Bonca Sep 26 '11 at 17:04
3 Answers
1
If you know the location of the cell then you can set it as
datagridview1[0,2].Value = true;// this should be a checkboxcolumn
OR
datagridview1["chkboxcolumnName",2].Value = true;
This will cause the checkbox to be checked for that particular cell.
Hope this is what you meant else please edit the question for more details.

V4Vendetta
- 37,194
- 9
- 78
- 82
0
Try it this way (by subscribing to two events):
void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
private void dataGridView1_CellValueChanged(object obj, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1) //compare to checkBox column index
{
DataGridViewCheckBoxCell cbx = (DataGridViewCheckBoxCell)dataGridView1[e.ColumnIndex, e.RowIndex];
if (!DBNull.Value.Equals(cbx.Value) && (bool)cbx.Value == true)
{
//checkBox is checked - do the code in here!
}
else
{
//if checkBox is NOT checked (unchecked)
}
}
}

Mitja Bonca
- 4,268
- 5
- 24
- 30
0
This worked for me:
for (int i = 0; i < dataGridView1.RowCount - 1; i++)
{
dataGridView1.Rows[i].DataGridView[0, i].Value = false;
}

Ayush Pateria
- 620
- 12
- 22