Home » Visual StudioRSS

DataGridView.DataSource throwing Index was out of range exception

Hello all,

I'm having a problem with DataGirdView. 

I am using a BindingSource object to bind that DataTable object to the DataGridView and I get the following exception thrown on the line I set the DataGirdView.DataSource = BindingSource object:-

Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"} System.Exception {System.ArgumentOutOfRangeException}

Heres the code:-

Code Snippet

source = newBindingSource();

source.DataSource = dset.Tables[0];

resultsGrid.DataSource = source;

I am at a loss as to why this is happening as the the DataSet contains 1 table and the Table contains 1 row.

After it throws the exception the app binds the table to DataGridView as expected 

Does anyone know why this would be happening ??

Thanks,
Sean

 

10 Answers Found

 

Answer 1

Maybe you can use Binding object  instead BindingSource and then write:

Binding binding = new Binding("you must have some parameters in constructor");
resultsGrid.DataBindings.Add(binding);

greet

 

Answer 2

Hi Dragoslav,

The thing is that BindingSource actually works. The only issue is that it thorws this exceptions before it binds the data.

I am trying to figure out why this is throwing this exception  for seemingly no reason.

Also I am unsure of how to use a Binding object. I've tried the following but its not working. Do you know what params I should be passing ??

Code Snippet

Binding source  = newBinding("resultsGrid.Rows", dset.Tables[0], "resultsGrid");

resultsGrid.DataBindings.Add(source);

 

Answer 3

I think I know what is happening. It is not problem  in BindingSource.
Did you put in datagridview  to autogenerate columns ?
Did you create columns in designer to match columns that comes from dset.Tables[0]

greet

 

Answer 4

Yes AutoGenerateColumns is set  to true, and I did not create columns in Form Designer

 

Answer 5

So did you experiment with these settings ?
Maybe you must disable autogeneratecolumns when you use BindingSource ?
Or try to disable autogenerate and create columns in desiger to match those that
come from dataset  (datatable) in code.
Set breakpoint and look structure of datatable  (click on litle magnifier icon in debug mode
when you point with mouse on datatable)

greet
 

Answer 6

Hi,

You can insert break point to debug. To find out which statement throw exception.

Maybe nothing to do with the BindingSource and dataGridView.

Hope helpful.

 

Answer 7

I just ended up removing MessageBox.Show(ex.Message) from the catch block. Like I said it works fine it just thorws this exception for some reason or another.

Not very professional I know but sure...... it works

 

Answer 8

Well that was the 1st thing I done. The exception  is thorwn at the line  when I assign the binding source  to the DataGridView.DataSource property.
 

Answer 9

Have you figured out why it threw this exception? I got exactly the same problem.

Thanks

 

Answer 10

I had a similar issue where I was getting the following error whenever I tried changing the datasource  on a DataGridView:

 

Index was out of range. Must be non-negative  and less than the size  of the collection.
Parameter name: index

 at System.Collections.ArrayList.get_Item(Int32 index)
 at System.Windows.Forms.DataGridViewColumnCollection.get_Item(Int32 index)
 at System.Windows.Forms.DataGridView.PositionEditingControl(Boolean setLocation, Boolean setSize, Boolean setFocus)
 at System.Windows.Forms.DataGridView.PerformLayoutPrivate(Boolean useRowShortcut, Boolean computeVisibleRows, Boolean invalidInAdjustFillingColumns, Boolean repositionEditingControl)
 at System.Windows.Forms.DataGridView.ResetUIState(Boolean useRowShortcut, Boolean computeVisibleRows)
 at System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged_PreNotification(CollectionChangeAction cca, Int32 rowIndex, Int32 rowCount, DataGridViewRow& dataGridViewRow, Boolean changeIsInsertion)
 at System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged(CollectionChangeEventArgs e, Int32 rowIndex, Int32 rowCount, Boolean changeIsDeletion, Boolean changeIsInsertion, Boolean recreateNewRow, Point newCurrentCell)
 at System.Windows.Forms.DataGridViewRowCollection.ClearInternal(Boolean recreateNewRow)
 at System.Windows.Forms.DataGridView.RefreshColumnsAndRows()
 at System.Windows.Forms.DataGridView.OnDataSourceChanged(EventArgs e)
 at System.Windows.Forms.DataGridView.set_DataSource(Object value)
 at OQADataImport.frmMain.<bsSelectedItem_DataSourceChanged>b__1c(Object o) in D:\SVN_Support\Code\OQM Projects\OQADataImport\OQADataImport\frmMain.cs:line 611

The solution we found was to make sure that the DataGridView was not in Edit mode when you are changing the datasource. The new code  we use to update the datasource is:

 

dgvResults.EndEdit();
dgvResults.DataSource = <New DataView>;

 

With this code the above error went away!

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter