none
DataGridView.DataSource throwing Index was out of range exception

    Question

  •  

    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 = new BindingSource();

    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

    Monday, February 25, 2008 10:02 AM

Answers

  • 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
    Monday, February 25, 2008 2:38 PM
  • 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

    Tuesday, February 26, 2008 9:10 AM

All replies

  • 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
             
    Monday, February 25, 2008 10:41 AM
  • 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 = new Binding("resultsGrid.Rows", dset.Tables[0], "resultsGrid");

    resultsGrid.DataBindings.Add(source);

     

     

    Monday, February 25, 2008 11:28 AM
  • 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

    Monday, February 25, 2008 1:07 PM
  • Yes AutoGenerateColumns is set to true, and I did not create columns in Form Designer

    Monday, February 25, 2008 2:13 PM
  • 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
    Monday, February 25, 2008 2:38 PM
  •  

    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.

    Tuesday, February 26, 2008 7:11 AM
  • 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

    Tuesday, February 26, 2008 9:10 AM
  • 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.
    Tuesday, February 26, 2008 9:30 AM
  • Have you figured out why it threw this exception? I got exactly the same problem.

    Thanks

     

     

    Monday, March 17, 2008 3:14 PM
  • 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!

    • Proposed as answer by Greg Bray Tuesday, July 13, 2010 12:44 AM
    Tuesday, July 13, 2010 12:42 AM
  • The below code solved my problem -

    resultsGrid.DataSource = null;
    resultsGrid.DataSource = source;

    Before assigning a new datasource, the grid should be unbinded with the previous datasource.

    • Proposed as answer by Priyam.Mitra Tuesday, November 19, 2013 2:53 PM
    Tuesday, November 19, 2013 2:53 PM