VJOY
VJOY

Reputation: 3792

Datagridview error System.IndexOutOfRangeException: Index 0 does not have a value


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.

Upvotes: 3

Views: 22221

Answers (5)

humbleviking
humbleviking

Reputation: 1

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.

Upvotes: 0

SM. Tabatabaei
SM. Tabatabaei

Reputation: 31

The problem has been solved by this code:

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

Upvotes: 3

Dirk V.
Dirk V.

Reputation: 151

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

Upvotes: 2

Arie
Arie

Reputation: 5373

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

Upvotes: 9

Gary.S
Gary.S

Reputation: 7121

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

Upvotes: 0

Related Questions