3


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.

5 Answers 5

9

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();
 }
1
  • this should be the accepted solution. assigning the list to a new list, and then rebinding it works but that is hacky. this correctly fixes the issue. and while another here also made the same suggestion - it lacked a good explanation and full code sample. +1. Feb 3, 2019 at 19:17
3

The problem has been solved by this code:

grdOrders.DataSource = null;
grdOrders.DataSource = this._controller.OrderActionData;
1
  • I had the same error when clearing a list was bound to a DataGridView. Setting the DataSource to null BEFORE clearing the list that was being used as the DataSource and then rebinding stopped the DataGridView from throwing this error. Aug 2, 2018 at 8:49
2

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();
0
0

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.