How to fix that stupid error - DataGridView System.IndexOutOfRangeException Index does not have a value ?

Posted on 2012-09-19
Last Modified: 2012-10-21

It is a new .NET 3.5 application in VisualStudio 2008.
I have added to a main form following components
DataSet ds,
BindingSource bsData // bsData.DataSource = ds
DataGridView dgvResultset // dgvResultset.DataSource = bsData

Then, I execute some WMI query, cleanup all from "ds" DataSet, then creating a new DataTable (TableName="Record") in the "ds" DataSet.
Then call some method to re-bind "ds" DataSet to a BindingSource and refresh DataGridView.

The thing I need - is read-only DataGridView object showing data from a DataTable object which is in the "ds" DataSet object.

The 1st data displaying works fine.

BUT(!!!) the problem is - on every next data refresh it is reporting lot of stupid errors:
20120919,192131.61 [T9]  DataGridView->Error(System.IndexOutOfRangeException): Index 2 does not have a value.
   at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
	   at System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32 rowIndex)

Open in new window

The "Index" value is usually different between data reloads. So, 1st data load is ok, on next data reload it reports N errors with Index = 0, then on next  data reload it reports N errors with Index = 4, then Index = 2 and so on. Seems Index value is walking in some random range.

I have placed my own DataError event handler, like this:
private void dgvResultset_DataError(object sender, DataGridViewDataErrorEventArgs e)
    e.Cancel = true;
    e.ThrowException = false;            
    Trace.WriteLineIf(TrcLvl.TraceError, TrcLvl.TraceError ? String.Format(
        ErrorUtils.FormatErrorMsg(e.Exception), ErrorUtils.FormatStackTrace(e.Exception)
        ) : "");

Open in new window

But that only suppressed an error, not solved it. So, all the time on next data re-loads it is reporting tons of that stupid errors. :-\

The questions are
how to get rid of that error?!
why that error is happening at all?! I assume if DataGridView is marked ReadOnly and BindingSource has AllowNew = false it should not try to do anything with Grid data, so should not be any reasons for mentioned errors...

Also - the main problem is not the mentioned error itself but that application is hanging after 3-5 query executions. I assume that is happened because of that error, somewhere inside a DataGridView. That is why I'm trying to figure out - how to get rid of that error.

Question by:Dmitry_Bond
    LVL 1

    Expert Comment

    The AllowNew property in the BindSource makes it so that no new data can be added to it's list.  This maybe causing the error because (if I remember right) the size of the Binding Source is used for the datagrid.  If new data is added, it is out of the index range.

    Accepted Solution

    I think it is not related to AllowNew property. At least - my tests not confirmed it.
    Finally, I was able to solve it using code like this:

    // this code to be in the Form_Load event handler:
    this.dgvResultset.AutoGenerateColumns = true;
    // this code to be used when re-binding to new DataTable
    if (pNewTable == null)
        this.bsData.DataMember = null;
        this.bsData.DataSource = null;
        this.bsData.DataSource = pNewTable;
        this.bsData.DataMember = "Record";

    Open in new window

    It is working for now. So, I'm happy. Assume issue is solved.

    Author Closing Comment

    finally problem was solved by myself.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now