简体   繁体   English

在DataAdapter.Fill(DataTable)期间触发RowChanged事件并引发错误

[英]RowChanged Event Fires and Throws an Error During DataAdapter.Fill(DataTable)

At the top of my code for the form I have the following statements (and some other unrelated ones to): 在表单代码的顶部,我有以下语句(和其他一些无关的语句):

Dim LastDataRow As DataRow
Public Event RowChanged As DataRowChangeEventHandler  

When my form loads I run the following line of code: 加载表单时,运行以下代码行:

AddHandler dt.RowChanged, New DataRowChangeEventHandler(AddressOf Row_Changed)

After the form loads I have a FetchData button which connects to the database and brings the records into a datatable object. 加载表单后,我有一个FetchData按钮,该按钮连接到数据库并将记录带入数据表对象。 During this fill operation my Row_Changed event fires (for every record I think) and tries to run DataAdapter.Update(DataTable) (pseudocode) which ultimately fails because I haven't yet created commands. 在此填充操作期间,我的Row_Changed事件触发(针对我认为的每条记录),并尝试运行DataAdapter.Update(DataTable) (伪代码),由于我尚未创建命令,该操作最终失败。 The error I get in my Row_Changed procedure is: 我在Row_Changed过程中遇到的错误是:

Update requires a valid InsertCommand when passed DataRow collection with new rows. 当传递带有新行的DataRow集合时,更新需要有效的InsertCommand。

I hadn't even planned to use an InsertCommand at all since I will not be allowing users to insert records from this form. 我什至根本没有计划使用InsertCommand,因为我不允许用户从此表单中插入记录。 I do plan to allow them to delete or modify existing records, but not insert new ones. 我确实计划允许他们删除或修改现有记录,但不插入新记录。

The only way I've found to work around this problem so far is to run my AddHandler statement after filling the datatable. 到目前为止,我发现解决此问题的唯一方法是在填充数据表后运行我的AddHandler语句。 However, I'm trying to figure out how to rewrite my code so that I can refresh the datatable without closing out the form/application and having to open it up again. 但是,我试图找出如何重写代码的方法,以便可以刷新数据表,而不必关闭表单/应用程序,而不必再次打开它。 I think that's a problem for a separate discussion. 我认为这是单独讨论的问题。

Any ideas why I'm getting the error message above? 为什么我收到上面的错误消息有任何想法吗?

You could RemoveHandler before calling the fill operation so Update does not get called. 您可以在调用填充操作之前使用RemoveHandler ,以便不会调用Update。 After the fill operation is complete, you can add the handler back again. 填充操作完成后,可以再次添加处理程序。

RemoveHandler
da.Fill(dt)
AddHandler

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM