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

Hi.

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(
        "DataGridView->{0}\n{1}",
        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.

Regards,
Dmitry.
code-pieces.cs
Dmitry_BondAsked:
Who is Participating?
 
Dmitry_BondAuthor Commented:
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.ResetBindings(true);
}
else 
{
    this.bsData.DataSource = pNewTable;
    this.bsData.DataMember = "Record";
    this.bsData.ResetBindings(true);
    this.bsData.CurrencyManager.Refresh();
}

Open in new window


It is working for now. So, I'm happy. Assume issue is solved.
0
 
koenigbrCommented:
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.
0
 
Dmitry_BondAuthor Commented:
finally problem was solved by myself.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.