Answered by:
datagridview with checkbox column

Question
-
i have a datagridview with checkbox column
i want to know the method that when i directly on check or uncheck the checkbox cellto do my code
i wrote this code but it's didnt work
{
{
{
}
}
}
can any on help me
Saturday, September 24, 2011 9:04 AM
Answers
-
Hi,
Try the following links
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellclick.aspx
Hope it helps.
Regards,
A.Murugan
If it solved your problem,Please click "Mark As Answer" on that post and "Mark as Helpful". Happy Programming!- Marked as answer by Dummy yoyoModerator Tuesday, October 4, 2011 2:44 AM
Saturday, September 24, 2011 9:26 AM -
Hi, make sure the cell *has* a value and ...
...(this will report the current state of the checkbox, so it will report when you change the state of the checkBox)
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { //maybe use a different ErrorContext if (e.RowIndex > -1 && this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null) MessageBox.Show(((DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]).GetEditedFormattedValue(e.RowIndex, DataGridViewDataErrorContexts.Formatting).ToString()); }
Regards,Thorsten
- Edited by Thorsten Gudera Saturday, September 24, 2011 9:28 AM
- Marked as answer by Dummy yoyoModerator Tuesday, October 4, 2011 2:44 AM
Saturday, September 24, 2011 9:27 AM -
... respectively (works also if the value previously was null):
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { //maybe use a different ErrorContext if (e.RowIndex > -1) MessageBox.Show(((DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]).GetEditedFormattedValue(e.RowIndex, DataGridViewDataErrorContexts.Formatting).ToString()); }
Regards,Thorsten
- Marked as answer by Dummy yoyoModerator Tuesday, October 4, 2011 2:44 AM
Saturday, September 24, 2011 9:34 AM -
To get the current state of the clicked checkBox, you will have to subscribe to two (2) event:
- CurrentCellDirtyStateChanged
- CellValueChanged
This is then the code which will get the current state of clicked checkedBox:
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 == 0) //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 (show a message as you did in your example! } else { //if checkBox is NOT checked (unchecked) } } }
Mitja- Marked as answer by Dummy yoyoModerator Tuesday, October 4, 2011 2:44 AM
Saturday, September 24, 2011 9:52 AM
All replies
-
Hi,
Try the following links
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellclick.aspx
Hope it helps.
Regards,
A.Murugan
If it solved your problem,Please click "Mark As Answer" on that post and "Mark as Helpful". Happy Programming!- Marked as answer by Dummy yoyoModerator Tuesday, October 4, 2011 2:44 AM
Saturday, September 24, 2011 9:26 AM -
Hi, make sure the cell *has* a value and ...
...(this will report the current state of the checkbox, so it will report when you change the state of the checkBox)
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { //maybe use a different ErrorContext if (e.RowIndex > -1 && this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null) MessageBox.Show(((DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]).GetEditedFormattedValue(e.RowIndex, DataGridViewDataErrorContexts.Formatting).ToString()); }
Regards,Thorsten
- Edited by Thorsten Gudera Saturday, September 24, 2011 9:28 AM
- Marked as answer by Dummy yoyoModerator Tuesday, October 4, 2011 2:44 AM
Saturday, September 24, 2011 9:27 AM -
... respectively (works also if the value previously was null):
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { //maybe use a different ErrorContext if (e.RowIndex > -1) MessageBox.Show(((DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]).GetEditedFormattedValue(e.RowIndex, DataGridViewDataErrorContexts.Formatting).ToString()); }
Regards,Thorsten
- Marked as answer by Dummy yoyoModerator Tuesday, October 4, 2011 2:44 AM
Saturday, September 24, 2011 9:34 AM -
To get the current state of the clicked checkBox, you will have to subscribe to two (2) event:
- CurrentCellDirtyStateChanged
- CellValueChanged
This is then the code which will get the current state of clicked checkedBox:
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 == 0) //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 (show a message as you did in your example! } else { //if checkBox is NOT checked (unchecked) } } }
Mitja- Marked as answer by Dummy yoyoModerator Tuesday, October 4, 2011 2:44 AM
Saturday, September 24, 2011 9:52 AM