Handling concurrency exceptions in DataGridView

穿精又带淫゛_ 提交于 2019-12-12 01:52:22

问题


This article here describes handling concurrency exceptions. The steps to reproduce the problem are:

  1. Create a new Windows Application project.
  2. Create a new dataset based on the Northwind Customers table.
  3. Create a form with a DataGridView to display the data.
  4. Fill a dataset with data from the Customers table in the Northwind database.
  5. After filling the dataset, use the Visual Database Tools in Visual Studio to directly access the Customers data table and change a record.
  6. Then on the form, change the same record to a different value, update the dataset, and attempt to write the changes to the database, which results in a concurrency error being raised.
  7. Catch the error, then display the different versions of the record, allowing the user to determine whether to continue and update the database, or to cancel the update.

My question is, why does this even happen? Why can't I just save and edit the record from the DataGridView without causing any errors? I'm creating an app with a DataGridView and I'm facing this problem. I need some way to avoid or resolve this error without notifying the user, so whatever they see in the DataGridView gets saved exactly the way thy see it. What's the cause of that error?


回答1:


The solution turned out to be pretty simple.

All you need to do is reload the data into the DataGridView again after every save.

So the code for the BindingNavigator save button is now:

this.Validate();
this.maintableBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.yourDataSet);
this.maintableTableAdapter.Fill(this.yourDataSet.yourtable);

I have no idea why this works, so I need an expert to confirm this. Working solution though.

Thanks to E-Bat for planting this idea in my head.



来源:https://stackoverflow.com/questions/30439187/handling-concurrency-exceptions-in-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!