在DataGridView中处理并发异常 [英] Handling concurrency exceptions in DataGridView

查看:74
本文介绍了在DataGridView中处理并发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本文描述了处理并发异常。重现此问题的步骤是:

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


  1. 创建一个新的Windows应用程序项目。

  2. 创建一个基于Northwind客户表的新数据集。

  3. 使用DataGridView创建表单以显示数据。

  4. 使用来自客户的数据填充数据集

  5. 填充数据集后,使用Visual Studio中的Visual Database Tools直接访问客户数据表并更改记录。

  6. 然后在表单上,​​将同一条记录更改为不同的值,更新数据集,然后尝试将更改写入数据库,这会引发并发错误。

  7. 捕获错误,然后显示记录的不同版本,使用户可以确定是继续并更新数据库,还是取消更新。

  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.

我的问题是,为什么会发生这种情况?为什么不能只从DataGridView保存和编辑记录而不会引起任何错误?我正在使用DataGridView创建应用,并且遇到了这个问题。我需要某种方法来避免或解决此错误而不通知用户,因此,无论他们在DataGridView中看到的内容如何,​​都将按照您的看到方式进行保存。

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?

推荐答案

解决方案非常简单。

您需要做的就是在每次保存后再次将数据重新加载到DataGridView中。

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

因此,BindingNavigator保存按钮的代码现在为:

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.

感谢 E-Bat 把这个想法植根于我的脑海。

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

这篇关于在DataGridView中处理并发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆