Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hey guys,
Weird issue. I am having a DataGridView with a CheckBox Column.
I am trying the set the values of the Checkbox column programmatically.
The issue is that all rows gets the check updated, but for some reason, the UI doesn't update the current row (mostly the selected one, but it still has the exception).
On debugging, I found that my code does every thing correctly. Here is the snippet -

C#
foreach (DataGridViewRow Row in dgvIgnoreApp.Rows)
{
    // CheckBoxColumn
    Row.Cells[0].Value = m_chkColumnHeaderIgnore.CheckAll; // Some Boolean value
}
dgvIgnoreApp.Invalidate();


I tried searching OL and the most I could go is where people suggested me to use DataGridView.CommitEdit() or DataGridView.EndEdit(). The new code looks like this -

XML
foreach (DataGridViewRow Row in dgvIgnoreApp.Rows)
{
    if (Row.Cells[0].Selected) // CheckBoxColumn
         dgvIgnoreApp.BeginEdit(false);

    Row.Cells[0].Value = m_chkColumnHeaderIgnore.CheckAll;

    if (Row.Cells[0].Selected)
         dgvIgnoreApp.EndEdit();
}
dgvIgnoreApp.Invalidate();


I used that but it is still giving me the same result.
Is it a MS .Net Bug? Help me guys...
Thanks in advance.
Posted
Updated 20-Sep-12 21:50pm
v2
Comments
Ank_ush 21-Sep-12 5:35am    
In which event have u written the above code?

I got a work around but don't know why this happened?

C#
foreach (DataGridViewRow Row in dgvIgnoreApp.Rows)
{    
    // CheckBoxColumn
    Row.Cells[0].Value = m_chkColumnHeaderIgnore.CheckAll; // Some Boolean value
}
dgvIgnoreApp.Enabled = false; //Miracle Line
dgvIgnoreApp.Enabled = true;  //Miracle Line


Discussion is still on for reasons and better answers.
Thanks you all.

--
Varun
 
Share this answer
 
Try this:-

Instead of this write this code:-
C#
Row.Cells[0].Value = m_chkColumnHeaderIgnore.CheckAll;


Write this:-
C#
Row.Cells[0].Value = CheckState.Checked;


Any problem please comment or reply.
 
Share this answer
 
Comments
Varun Pandey 21-Sep-12 8:55am    
Just to help - a very interesting Workaround, disabling & enabling DataGridView after modifying the rows resolves this issue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900