Windows Develop Bookmark and Share   
 index > Windows Forms General > editing datasets using navigator save button
 

editing datasets using navigator save button

when i load an access datatable, using datagridview, the 'save' button on the navigator does not appear. Changesto data items made in the datagridview are not saved. How can I fix this?
I think i have this solved. Have added a 'save' button by hand.
new trouble is that this code:
(Dataset is 'budget', datatable being edited is 'transactions')

...

transactionsBindingSource.EndEdit();

budgetDataSet.transactions.AcceptChanges();

transactionsTableAdapter.Update(budgetDataSet);
...

(and variations) does not save the changes made.

How do i ensure that changes are actually saved?
thanks

  • Moved byTaylorMichaelLMVPMonday, September 21, 2009 1:38 PMWinForms related (From:Visual C# IDE)
  •  
sushu  Monday, September 21, 2009 5:27 AM

Hi sushu,

Sorry for the late reply. The root cause is that you call the AcceptChanges method of the DataTable before the data is updated to database. The msdn document said:
When AcceptChanges is called, any DataRow object still in edit mode successfully ends its edits. The DataRowState also changes: all Added and Modified rows become Unchanged, and Deleted rows are removed.

So the edit states are lost, the DataAdapter would think there is not row modified, added or deleted and the data would not be updated to database. You can get more about AcceptChanges from: http://msdn.microsoft.com/en-us/library/system.data.datatable.acceptchanges.aspx

To solve the issue, we need to remove the calling of the AcceptChanges method as follows:
private void saveToolStripButton_Click(object sender, EventArgs e)

{

Validate();

dataGridView1.EndEdit();

transactionsBindingSource.EndEdit();

transactionsTableAdapter.Update(budgetDataSet);

}

By the way, the id column is auto incremented, so we’d better have this column read only to disallow the user to fill some useless values. We also need to add a refresh button to allow user to fetch the data from database again to see if the data is updated successfully.

Let me know if this does not help.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, October 01, 2009 4:26 AM

Hi sushu,

You need to trace and debug the program to figure out if the values in the DataGridVieware updated to the binding source. If not, we need to call the EndEdit method of the DataGridView to force it update the underlying binding source.

You can get more about EndEdit of the DataGridView from:
http://msdn.microsoft.com/en-us/library/ms158620.aspx.

Regards,
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Tuesday, September 22, 2009 9:10 AM
Hi Aland Li,
thanks for suggestion. does not work though. The datagridview accepts the changed value on the screen- can be a simple as changing a 'C' to an 'S'. But the changed value is not stored in the underlying dataset. Frustrating!

Validate();

dataGridView1.EndEdit();

transactionsBindingSource.EndEdit();

budgetDataSet.transactions.AcceptChanges();

transactionsTableAdapter.Update(budgetDataSet);


sushu  Wednesday, September 23, 2009 3:19 AM
Hi sushu,

Could you please package your code in a small project in which the issue can be reproduced and email it to me: alala666888@hotmail.com ?

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, September 30, 2009 2:18 AM

Hi sushu,

Sorry for the late reply. The root cause is that you call the AcceptChanges method of the DataTable before the data is updated to database. The msdn document said:
When AcceptChanges is called, any DataRow object still in edit mode successfully ends its edits. The DataRowState also changes: all Added and Modified rows become Unchanged, and Deleted rows are removed.

So the edit states are lost, the DataAdapter would think there is not row modified, added or deleted and the data would not be updated to database. You can get more about AcceptChanges from: http://msdn.microsoft.com/en-us/library/system.data.datatable.acceptchanges.aspx

To solve the issue, we need to remove the calling of the AcceptChanges method as follows:
private void saveToolStripButton_Click(object sender, EventArgs e)

{

Validate();

dataGridView1.EndEdit();

transactionsBindingSource.EndEdit();

transactionsTableAdapter.Update(budgetDataSet);

}

By the way, the id column is auto incremented, so we’d better have this column read only to disallow the user to fill some useless values. We also need to add a refresh button to allow user to fetch the data from database again to see if the data is updated successfully.

Let me know if this does not help.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, October 01, 2009 4:26 AM
Hi Aland Li,
the changesolved the problem for me. Thanks a lot for your help, and for the other useful suggestions.
Hugh
sushu  Thursday, October 01, 2009 11:12 PM

You can use google to search for other answers

Custom Search

More Threads

• MouseMove event
• TreeView Search Functionality in WPF (using C# and XAML)
• a splash screen
• complex TreeView best practises
• MaskedTextBox DateTime format
• Choose a font with FontDialog
• Reproducing legacy behavior
• How to set AcceptsReturn for a datagridview text column
• Windows Forms application displays differently on different computer OS
• Right-align text in combobox