datagrid
how to generate events for datagrid checkboxes in windows application
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Ashwini PisalPosted Aug 28, 2012, 2:18 PM
You can handle DataGridView's CellValueChanged Event Handler
The CellValueChanged event gets fired whenever the value of a DataGridView cell is changed. Here, theRowCheckBoxClick method is invoked by passing a reference of DataGridViewCheckBoxCell that raised this eventprivate void dgvSelectAll_CellValueChanged(object sender, DataGridViewCellEventArgs e) { RowCheckBoxClick((DataGridViewCheckBoxCell)dgvSelectAll[e.ColumnIndex, e.RowIndex]); }
This method checks / unchecks the header CheckBox's state depending upon whether all CheckBoxes of aDataGridView column are checked or unchecked
private void RowCheckBoxClick(DataGridViewCheckBoxCell RCheckBox) { if (RCheckBox != null) { //Modify Counter; if ((bool)RCheckBox.Value && TotalCheckedCheckBoxes < TotalCheckBoxes) TotalCheckedCheckBoxes++; else if (TotalCheckedCheckBoxes > 0) TotalCheckedCheckBoxes--; //Change state of the header CheckBox. if (TotalCheckedCheckBoxes < TotalCheckBoxes) HeaderCheckBox.Checked = false; else if (TotalCheckedCheckBoxes == TotalCheckBoxes) HeaderCheckBox.Checked = true; } }