简体   繁体   English

VB.net 2010-刷新数据集时来自datagridview的IndexOutOfRange

[英]VB.net 2010 - IndexOutOfRange from datagridview when refreshing dataset

I have a datagridview on a form that is bound to a table in a dataset from another class. 我在绑定到另一个类的数据集中的表的表单上有一个datagridview。

I use a data adapter to .Fill a table in that dataset and the grid displays the data fine. 我使用数据适配器来填充该数据集中的表格,并且网格可以很好地显示数据。

Works fine. 工作正常。

On my form I have a textbox the user can type in that will will pass a parameter to the storedprocedure used to fill this table. 在我的表单上,我有一个用户可以键入的文本框,该文本框会将一个参数传递给用于填充此表的storedprocedure。 So on startup the textbox will have "%" in it. 因此,在启动时,文本框将包含“%”。 and then the user can type in "F%" and get everything that starts with an "F" 然后用户可以输入“ F%”并获取以“ F”开头的所有内容

So when that textbox changes I launch an async refresh (.BeginInvoke) to do my refresh. 因此,当该文本框发生更改时,我将启动异步刷新(.BeginInvoke)进行刷新。 The table gets populated with the reduced number of records (I check ds.table(0).rows.count and it is correct) 该表填充了减少的记录数(我检查了ds.table(0).rows.count,它是正确的)

But the datagridview then starts throwing datagridview.dataerror events. 但是datagridview然后开始引发datagridview.dataerror事件。 "System.IndexOutofRangeException : Index # does not have a value" “ System.IndexOutofRangeException:索引号没有值”

It looks like the dataset is getting filled correctly and not having any issues, but the datagrid is not liking this update. 看起来数据集已正确填充并且没有任何问题,但是datagrid不喜欢此更新。 The index out of range error is so common that I'm not finding what I need through searches. 索引超出范围错误非常常见,以至于我无法通过搜索找到所需的内容。

Thanks in advance! 提前致谢! :) :)

I don't know the solution to your specific problem, but it seems like a waste to hit the database for every keystroke when all the data is loaded up front. 我不知道您的特定问题的解决方案,但是当所有数据都预先加载时,每次击键都访问数据库似乎是一种浪费。

Rather than requerying the database every time, why not set the RowFilter of the DataView and skip the database call? 为什么不设置DataViewRowFilter并跳过数据库调用,而不是每次都重新查询数据库?

Well, find the error. 好吧,找到错误。

  • Take out the BeginInvoke and do it synchronous. 取出BeginInvoke并使其同步。 See if that fixes it. 看看是否能解决问题。 If not, leave it out until you're sure you have no other problems. 如果没有,请在确定没有其他问题之前将其保留。

  • @roadie has a good comment on resetting the SelectedRow @roadie对重置SelectedRow有很好的评论

  • There are similar ways to enforce the update. 有类似的方法可以强制执行更新。 Like 喜欢

.. ..

   bindingSource.DataSource = null; 
   bindingSource.DataSource = myTable;

I had a similar problem and it was because I wanted to do this: 我有一个类似的问题,这是因为我想这样做:
dg1.CurrentCell = dg1.Rows(dg1.RowCount - 1).Cells(1) dg1.CurrentCell = dg1.Rows(dg1.RowCount-1).Cells(1)
after changing dg1.DataSource. 更改dg1.DataSource之后。
It seems to be dangerous to use RowCount when AllowUserToAddRows = True. 当AllowUserToAddRows = True时,使用RowCount似乎很危险。
I switched it to False and it worked. 我将其切换为False,并且有效。

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

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