Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

When I am updating cell value in a datagridview it is not updating regularly.One

of the control in my datagrid is checkbox.When I check or uncheck the checkbox and when I

debug the event CellClick it is getting correct cell value(That is false or true

respectievely).Immediate after

without clicking any other checkbox when I click that same checbox it is not getting the

correct value but getting the same value as before(That is false or true respectievely)as

before.But the checkbox is showing unchecked or checked respectievely).But after

clicking any of the other check box and if I am clicking that checkbox again it is getting

the correct value.Please help me.I am having a trouble with this issue

Thanks in advance .
Posted
Updated 21-Sep-22 1:05am

When a check box of DataGridView is clicked, it will be in dirty state and when another cell or row is clicked the data will be committed. In your case when you click the check box and do not click elsewhere then the CurrentCell will be in dirty state. Handle the following event as shown below to commit the data

C#
void dataGridView1_CurrentCellDirtyStateChanged(object sender,
EventArgs e)
{
    //replace ColumnIndex == 1 in the following code with column index of your check box column
    if (dataGridView1.CurrentCell.ColumnIndex== 1 && dataGridView1.IsCurrentCellDirty)
    {
    	dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
}


You may accept and vote solution if your problem is solved, otherwise post your queries.
 
Share this answer
 
v2
Comments
kutz 2 5-Mar-12 6:09am    
First upall thank you for your consideration.
In this code using 'e' I can't get the current columnIndex.I mapped the event CurrentCellDirtyStateChanged and used only the code

dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
But still issue is happening.
Also I am asking why it is using

*****if (dataGridView1.CurrentCell.ColumnIndex== 1 && dataGridView1.IsCurrentCellDirty)********
kutz 2 5-Mar-12 6:20am    
Please note that "CurrentCellDirtyStateChanged" event is happening before CellContentClicked event
ProEnggSoft 5-Mar-12 6:30am    
'e' does not have the ColumnIndex property. That's why dataGridView1.CurrentCell.ColumnIndex == 'your check box column index' is to be used to check whether the current cell is check box and IsCurrentCellDirty will be true if the cell is edited but not yet committed. Hence, use
if (dataGridView1.CurrentCell.ColumnIndex== 1 && dataGridView1.IsCurrentCellDirty)

and check. Replace 1 in this statement with the column index of your check box column.
ProEnggSoft 5-Mar-12 6:31am    
dataGridView1.IsCurrentCellDirty is important to check whether cell is clicked but not yet committed.
kutz 2 8-Mar-12 1:13am    
Yes now it is working fine.Thank you very much
Hello freind...


As you say you cant see the updated value to your datagridview.right...

so you can try this i hope it will solve your problem...


at the end of checkbox or any event which will affect your gridview cell value you need to put one more line at the end of your logical code like this...
C#
DataGridView1.Refresh();


i hope it will work for you....
 
Share this answer
 
Comments
Tejas Vaishnav 5-Mar-12 4:43am    
don't forgot to mark as solve if your problem is solve and also rate answer so other will use it for their problem solving..
kutz 2 5-Mar-12 5:04am    
I already used this line.But still the problem happening.Suppose when I check a checkbox it will show as checked but immediate after when I uncheck the same checkbox without clicking anyother checkbox the earlier cell value is still false.If I click any of the other checkbox and then again the first checkbox I can get the correct value.If you have any confusion let me know.
Member 10027976 2-Jul-13 5:26am    
hi
My best solution of all found in the net:
datagridview1.Rows.Add();
datagridview1.Rows.RemoveAt(datagridview1.RowCount -1);
 
Share this answer
 
use datagridview1.AcceptChanges()


this is working fine. tested it.


use and give ur feedback :D
 
Share this answer
 
Comments
CHill60 2-Jul-13 9:41am    
Problem was resolved a year ago

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