.Net DataGridView“索引0没有值”。 [英] .Net DataGridView "Index 0 does not have a value."

查看:3121
本文介绍了.Net DataGridView“索引0没有值”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个DataGridView问题。
尝试删除其中一个项目并重新加载网格时,我收集了3个绑定到网格的项目。



如果有代码



  If(dlg.ShowDialog()= DialogResult.OK)然后
'显示对话框与网格
结束如果

在打开的对话框中,我从网格中删除一个项目(应该依次删除)从集合中的项目,并重新加载网格),并返回到If(dlg.show ...行,错误



System.Windows.Forms.dll中出现类型System.IndexOutOfRangeException的第一次机会异常

附加信息:索引2没有值。



(我已经打破了调试器设置通用语言运行时错误)



我可以理解这个错误,如果我是尝试访问任何单元格,行或列,但不是,然后我会期望异常停止在代码行,试图acc ess这个网格数据,而不是If(dlg.ShowDialog()...行



任何想法?
Cheers

解决方案

我发现如果你添加了

  DataGridView.DataSource = Nothing 
DataGridView.Refresh()


$ b在将网格重置为集合(删除已删除的项目)之前$ b



  DataGridView.DataSource = MyCollection 

它的作用就像一个魅力。
仍然有兴趣了解为什么你必须这样做。


I am having trouble with a DataGridView. I have a collection of 3 Items bound to the grid, when trying to delete one of the items and reload the grid.

If have code of

If (dlg.ShowDialog() = DialogResult.OK) Then
     'Show dialog with grid on it
End If

On the opened dialog, I delete an item from the grid (which should in turn, delete the item from the collection, and re-load the grid), and it returns to the "If (dlg.show..." line, with the error of

"A first chance exception of type 'System.IndexOutOfRangeException' occurred in System.Windows.Forms.dll

Additional information: Index 2 does not have a value. "

(I have break into debugger set on for common language runtime errors)

I can understand this error if i were trying to access any cells, row or columns, but im not, and then I would expect the exception to stop on the line of code that is trying to access this grid data, not the "If (dlg.ShowDialog()... " line

Any ideas? Cheers

解决方案

I have discovered that if you add in

DataGridView.DataSource = Nothing
DataGridView.Refresh()

before resetting the grid to the collection (with the deleted item removed)

DataGridView.DataSource = MyCollection

It works like a charm. Still interested as to why you have to do this.

这篇关于.Net DataGridView“索引0没有值”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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