Need help with checking/unchecking datagridviewcheckboxcolumn in C#
-
Monday, May 30, 2011 2:45 AM
Hi, I'm really having difficulty with this. Can someone pls help me why this does not work?
private void dataGridViewempl_CellClick(object sender, DataGridViewCellEventArgs e) { if (dataGridViewempl.CurrentCell.ColumnIndex == 6) { if (dataGridViewempl.CurrentCell.Value != null) { bool checkstate = (bool)dataGridViewempl.CurrentCell.Value; if (checkstate == false) dataGridViewempl.CurrentCell.Value = true; else dataGridViewempl.CurrentCell.Value = false; } else dataGridViewempl.CurrentCell.Value = true; }OR this?
private void dataGridViewempl_CellClick(object sender, DataGridViewCellEventArgs e) { CheckBox deletebox = new CheckBox(); deletebox = (CheckBox)dataGridViewempl.CurrentCell.Value; if (dataGridViewempl.CurrentCell.ColumnIndex == 6) { if (deletebox != null) { if (deletebox.Checked == false) deletebox.Checked = true; else deletebox.Checked = false; } }Appreciate any help!
All Replies
-
Monday, May 30, 2011 4:53 AM
Hi,
Use this,
if (dataGridViewempl.CurrentCell.ColumnIndex == 6)
{
if (dataGridViewempl.CurrentCell.Value != null)
{
bool checkstate = (bool)dataGridViewempl.CurrentCell.GetEditedFormattedValue(e.RowIndex,DataGridViewDataErrorContexts.Commit);
if (checkstate == false)
dataGridViewempl.CurrentCell.Value = true;
else
dataGridViewempl.CurrentCell.Value = false;
}
else
dataGridViewempl.CurrentCell.Value = true;
Hope this helps you..
Regards, Kris. -
Monday, May 30, 2011 10:31 AM
Hi Kris444,
Thanks! But it still doesn't work. I'm really confused. Does it have something to do with some properties of my datagrid? I set the EditMode to EditOnEnter, and either of my code above works. But if it's EditProgrammatically, none of it (and yours) work at all.
-
Tuesday, May 31, 2011 8:37 AM
Hi Chair88,
Based on your code, I am not fully understand what readlly do you want to achieve.
But for EditMode, the EditOnEnter mode means that editing begins when the cell receives focus, this mode is useful when pressing the TAB key to enter values across a row, or when pressing the ENTER key to enter values down a column.
And the EditProgrammatically means Editing begins only when the BeginEdit method is called.
As you don't fire the BeginEdit method, so it will not work for you.
You can also refer to this official document to fully understand the DataGridViewEditMode Enumeration:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridvieweditmode.aspx
Vin Jin [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

-
Thursday, June 02, 2011 6:02 AM
Hi,
Thanks all for your time. Actually I have solved this with help from the other forum I have visited.
@Vin Jin, I was trying to make the checkbox inside my datagridview check/uncheck by clicking the cell. I previously thought it had something to do with the EditMode property. Just found out that the code I'm using (where I cast the checkbox cell into a boolean type and get its value "true" (checked) or "false" unchecked)) does not work because it lacks this line of code:
dataGridViewempl.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(dataGridViewempl_CellClick);
Thanks much!- Marked As Answer by Aspen VJ Tuesday, June 07, 2011 2:13 AM
-
Thursday, June 02, 2011 6:58 AM
Hi Chair88,
Congratulation! I am glad that you have solved your problem. Thanks for sharing your solution, and please mark it as answer. Thanks.
Vin Jin [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


