Datagridview错误System.IndexOutOfRangeException:索引0没有值

发布于 2024-12-09 22:43:51 字数 456 浏览 1 评论 0原文


当我尝试填充绑定源时遇到一个错误。例外情况如下;

System.IndexOutOfRangeException: Index 0 does not have a value.
   at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
   at System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32 rowIndex)

我正在使用通用列表来填充绑定源。代码看起来像,

foreach (listItem)
  {
      BindingSource.Add(listItem);
  }

我尝试重置数据源属性,但仍然是同样的问题。

请帮我解决这个问题。

I am getting one error when I am trying to populate binding source. The exception is as follows;

System.IndexOutOfRangeException: Index 0 does not have a value.
   at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
   at System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32 rowIndex)

I am using generic list to fill binding source. The code looks like,

foreach (listItem)
  {
      BindingSource.Add(listItem);
  }

I tried resetting the datasource property, but still the same issue.

Please help me to resolve this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

多像笑话 2024-12-16 22:43:51

据我了解,您不必填充 BindingSource,只需填充它绑定到的列表即可。这就是绑定的全部思想。您可以使用绑定源将控件绑定到数据。

然后

myBindingSource.DataSource = listItem;

就会去做。

另外,您可以将 datagridview 绑定到 BindingList,而不是将 datagridview 绑定到 BindingSource 并将 BindingSource 绑定到列表。它与 List 类似,但也实现了 IBindingList 接口(当您将 Bi​​ndingList 对象设置为 List 时,它将返回一个实现 IBindingList 的对象,因此非常相似)

因此您可以这样做:

myDataGridView.DataSource = myBindingList;

如果 myBindingList 上的项目的属性发生更改,默认情况下,结果将反映在 datagridview 上,如果集合发生更改(添加或删除了某些内容),您可以使用以下命令刷新它:

 CurrencyManager cm = (CurrencyManager)this.myDataGridView.BindingContext[myBindingList];
 if (cm != null)
 {
    cm.Refresh();
 }

As far as I understand, you don't have to populate BindingSource, you just have to populate the list it's bound to. That's the whole idea of binding. You bind your control to the data using bindingsource.

And then

myBindingSource.DataSource = listItem;

will do it.

Also, instead of binding your datagridview to BindingSource and your BindingSource to list, you can just bind your datagridview to BindingList. It is similar to List, but also implements IBindingList interface (when you set the BindingList object to List, it will return an object implementing IBindingList, so it'll be very similar)

Sou you can do:

myDataGridView.DataSource = myBindingList;

If properties of items on myBindingList change, the result will be reflected on datagridview by default, if the collection changed (some things were added or deleted), you may refresh it using:

 CurrencyManager cm = (CurrencyManager)this.myDataGridView.BindingContext[myBindingList];
 if (cm != null)
 {
    cm.Refresh();
 }
醉态萌生 2024-12-16 22:43:51

该问题已通过以下代码解决:

grdOrders.DataSource = null;
grdOrders.DataSource = this._controller.OrderActionData;

The problem has been solved by this code:

grdOrders.DataSource = null;
grdOrders.DataSource = this._controller.OrderActionData;
浅紫色的梦幻 2024-12-16 22:43:51

当列表不再与 DataGridView 同步时,会发生错误。

您可以在列表更改后手动刷新绑定,以确保绑定再次同步:

myBindingSource.CurrencyManager.Refresh();

The error occurs when the list is no longer synchronized with the DataGridView.

You can manually refresh the bindings after the list has been changed to ensure that the bindings are synchronized again:

myBindingSource.CurrencyManager.Refresh();
如梦初醒的夏天 2024-12-16 22:43:51

我在这里在黑暗中拍摄,但假设这是伪代码,那么您需要将 UI 元素的数据源设置为绑定源。另外,这样做可能更容易:

var binding = new BindingSource();
binding.DataSource = listItem;
DataGridView.DataSource = binding;

有关 BindingSource 的更多信息可以在此处找到:http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.aspx

I am shooting in the dark here but assuming that is pseudo code then you need to set the datasource of a UI element to the binding source. Also, it may be easier to just do something like this:

var binding = new BindingSource();
binding.DataSource = listItem;
DataGridView.DataSource = binding;

More info regarding BindingSource can be found here: http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.aspx

才能让你更想念 2024-12-16 22:43:51

我只是在关闭表单之前设置 ItemsBindingSource.DataSource = Nothing (“Items”是表的名称)。所以我...

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    ItemsBindingSource.DataSource = Nothing
    Me.Close()
End Sub

可能不正确,但我没有收到错误。

I just set ItemsBindingSource.DataSource = Nothing ("Items" is the name of the table) right before I close the form. So I have...

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    ItemsBindingSource.DataSource = Nothing
    Me.Close()
End Sub

May not be right, but I don't get the error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文