Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Datagridview does not commit changes made to DataTable in Vista
 

Datagridview does not commit changes made to DataTable in Vista

I have a datagridview which is bound to a datatable. The datagrid has its AllowUsers to add new rows and delete rows property set to true. The users can add or delete rows from the datagridview. Also the editmode of the datagridview is set to EditOnEnter. Consider the following situation:
1) I make some edits and entries to a row in the datagridview.
2) Without moving the cursor to the second row (i.e the cursor is currently on the row which has been edited), if I try to save the changes made, the changes are not saved.
3) But if I click on any other row, and then try to save, the changes are saved.
This behaviour of datagridview only occurs in Windows Vista. The same application when run in Windows XP works fine. Any ideas?


Thanks
Priya Vaithianathan  Tuesday, August 05, 2008 7:07 AM

Hi Priya Vaithianathan,

I am not sure about what you mean by “not saved� Did you mean the changes are not update to database, or did you mean the changes are not committed to underlying data list (DataTable)?

I performed a test to save the changes to database, but I cannot reproduce the problem on Windows Vista. I guess you may forget to validate the changes. Validation is a required part of updating the data in a bound control. Once the form and all bound controls are validated, any current edits need to be committed. Finally, the table adapter needs to push its changes back to the database. To do this, put the following three lines in the save event handler on your form:

Code Snippet

this.Validate();

this.customersBindingSource.EndEdit();

this.customersTableAdapter.Update(this.northwindDataSet.Customers);

Best regards.
Rong-Chun Zhang

Windows Forms General FAQs
Windows Forms Data Controls and Databinding FAQs

Rong-Chun Zhang  Thursday, August 07, 2008 6:46 AM
I meant the underlying datatable when I said "not saved". This datatable is an intermediate which is pushed back to another class which takes care of the database update. Therefore there is no dataadapter or dataset involved here (another class just returns this datatable). The problem I am facing is, when I edit a row in the datagridview, only when come to the next row of the datagrid the first row is pushed into the datatable. If I edit the row's cells one by one and keep my cursor on the same row, then the data entered is not pushed into the datatable. The datatable's RowChanged and RowChanging event are called when the datagrid loses focus in XP (i.e when I click Ok to save changes). But in Vista the event fires only when the cursor is shifted to the next row in datagridview. This is a WPF application and I am using WindowsFormsHost to host the datagridview.
Priya Vaithianathan  Saturday, August 09, 2008 4:49 AM

HiPriya Vaithianathan,

It seems it is a question about focus. The changes made in a DataGridView aren’t committed until you move to another row. To work around this, we can try to use DataGridView.CommitEdit to manually commit the changes and call EndEidt for the BindingSource object.

Code Snippet

if (this.dataGridView1.IsCurrentCellDirty)

{

this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);

bindingSource.EndEdit();

}

Let me know if this helps, if not, could you please show us of your code to reproduce your problem?

Best regards.
Rong-Chun Zhang

Windows Forms General FAQs
Windows Forms Data Controls and Databinding FAQs

Rong-Chun Zhang  Monday, August 11, 2008 8:14 AM
Where are you using this code snippet? In CellValueChanged or CellValidating?
Priya Vaithianathan  Thursday, August 14, 2008 4:51 AM

You can try to use the above code in the save button click event handler

Best regards.
Rong-Chun Zhang

Windows Forms General FAQs
Windows Forms Data Controls and Databinding FAQs

Rong-Chun Zhang  Thursday, August 14, 2008 7:53 AM
Hi. I have gone through your article and made changes accordingly. I added these lines:
this.Validate();
this.bindingSource1.EndEdit();
this.adapter.Update(this.table);

These changes are not reflected into database. Where am i making mistake?
Initailization:
BindingSource bindingSource1 = new BindingSource();
DataTable table = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter();//adapter initialized
Manvendra Kumar  Tuesday, September 09, 2008 3:25 AM
I have got the same problem and used the below code but its not working

if (this.dataGridView1.IsCurrentCellDirty)

this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);

Akhil.N.P

Akhi4Akhi  Wednesday, February 04, 2009 6:07 AM
What happened to this discussion? It stopped mid way with no answer.
NZeek  Wednesday, September 02, 2009 3:48 PM

I had this same problem, and this is how I fixed it:

Me.Validate()
MyDataTable.EndEdit()
MyAdapter.Update(MyDataTable)

DO NOT call MyDataTable.AcceptChanges before running this code. That call sets the state of all rows in the DataTable to Unchanged, so the adapter doesn't see anything that needs to be updated.

This is how Rong-Chun explained it above in his first response and it seems to make sense, and it works. I'm using Vista 64-bit, .NET 2.0, VS 2008.

AerialPhotoPro  Friday, September 18, 2009 9:35 PM

You can use google to search for other answers

Custom Search

More Threads

• stored procedures
• Random strangeness in a bound form
• Implementing IPropertyChanged and IEditableObject in a lightweight wrapper Class?
• Binding Textboxs to SQLDatabase
• Databinding two comboboxes to same source
• Tables & Views - Have to submit this wednesday :(
• datagridview +
• Updating the underlying table of a views in datagridview
• Download entire folder via webservices
• why is this wrong