?
Solved

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

Posted on 2012-09-19
3
Medium Priority
?
7,881 Views
Last Modified: 2012-10-21
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
0
Comment
Question by:Dmitry_Bond
  • 2
3 Comments
 
LVL 1

Expert Comment

by:koenigbr
ID: 38433321
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
 

Accepted Solution

by:
Dmitry_Bond earned 0 total points
ID: 38504287
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
 

Author Closing Comment

by:Dmitry_Bond
ID: 38518574
finally problem was solved by myself.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

840 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