Hi All,

Honestly I couldn't figure out where to ask this question so I am trying my chance here.

I am getting System.IndexOutOfRangeException in Application.Run(new frmMain); line. Since this much information didn't make sense to me having this exception in the main method and I couldn't succeed to reproduce the error, I checked stack trace for further information on the error.

here it is;


System.IndexOutOfRangeException was unhandled
Message="Index -1 does not have a value."
at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
at System.Windows.Forms.CurrencyManager.get_Current()
at System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnRowEnter(DataGridViewCellEventArgs e)
at System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell& dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean canCreateNewRow, Boolean validationFailureOccurred)
at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)
at System.Windows.Forms.DataGridView.OnCellMouseDown(HitTestInfo hti, Boolean isShiftDown, Boolean isControlDown)
at System.Windows.Forms.DataGridView.OnCellMouseDown(DataGridViewCellMouseEventArgs e)
at System.Windows.Forms.DataGridView.OnMouseDown(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.DataGridView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at MyApplication.Program.Main() in C:\Dokumente und Einstellungen\xxxx\Eigene Dateien\Visual Studio 2005\Projects\MyApplication\MyApplication\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()


From stack trace information I have the impression of that datagridview control on the child form of mdiform (frmMain) causing this. Here is the situation in my Application.

I have frmMain as mdicontainer and frmPart as mdichild. in frmPart I have a combobox that feeds first datagridview and a second datagridview control which acts in a master-detail relation with the first one.

Also in the second dgv I have 3 checkboxcolumns where I handled the cellcontentclick event to get checkboxes on the same row act like a group of radio buttons. Some values in the first dgv are getting updated according to selections in second datagridview.

I hope I didn't messed up the question text because I already messed up my application. Another messup is the last thing I need now Smile

Can anyone tell me what may be causing getting this exception or error in the main method of the program



Re: Windows Forms General System.IndexOutOfRangeException in Application.Run(new frmMain); line.

Christopher Payne

Looking at the call stack, the error occured during a CurrencyManager.Get_Current() call. An index out of range exception here means that you are reading the CurrencyManager.Current property without first making sure that the CurrencyManager.Position > -1.

The assumption we like to make is that Current will be null if there is no current record...but instead it throws the Index Out of Range exception.

Do you have a handler for the RowEnter event That's where I would start looking. Failing that, look for any place that you are assuming there is a record present to bind to.

Are you using code to sync your two DataGridViews Or do you have a relationship defined in your DataSet I find it is much easier if you use a relationship in the DataSet, although you do have to get the bindings correct for it to work.

Re: Windows Forms General System.IndexOutOfRangeException in Application.Run(new frmMain); line.


Hi Christopher,

Thanks for reply. I don't have any handler for RowEnter event. I am using collections as datasource for the grids and during runtime I use my own code to maintain sync between to DataGridViews. It seems like my code is not quite successful in syncronizing two DataGridViews. May be I should go for a relationship based solution for this. Could you help me with creating a relationship between two collections


Finally I reproduced the error. Here is the story:

At the begining everything works as expected then,

1- I am clearing the master DataGridView (programaticaly via a button on the form)

2- Closing and reoppening the form (mdichild) (on close I set datasource collections to null and onload repopulating)

3- Adding an item into master DataGridView.

As soon as an item added into DataGridView when I click on the column headers I am getting this error. Interesting part

- If the form is loaded for first time clicking column headers does not have this effect

- if the form is loaded for first time clearing DataGridView and adding new items does not cause this behaviour it only happens just after the reopening.

- If the form reopened with an empty DataGridView still there is no problem

- if the form reopened with some items in DataGridView still there is no problem

Re: Windows Forms General System.IndexOutOfRangeException in Application.Run(new frmMain); line.

Christopher Payne

DataSet objects have built-in support for relations, whereas collections don't. So I don't think you can do the binding quite the same way as you could if you were binding to a DataSet.

Try commenting out your code that responds to the content cell click event. If that makes the problem go away, then perhaps the click event code needs to check to make sure the click isn't in the headers before it tries to access the row.

Also look closely at your code that fills your collections. Whenever you search for a value, make sure you actually find what you are looking for instead of just assuming there is a match. This error does lead me to believe that you are ending up with an orphan in your child collection when you try to re-use the form.

Re: Windows Forms General System.IndexOutOfRangeException in Application.Run(new frmMain); line.


I think you were right at the first place. Whenever I set an empty datasource to the DataGridView the CurrencyManager's value becomes -1. And then even if I add an item to the datasource collection still it remains -1. I tried to increment it but didn't work. Can you provide me some code snippets on how to change the currencymanager's value after adding an item to the datasource collection


Re: Windows Forms General System.IndexOutOfRangeException in Application.Run(new frmMain); line.

Christopher Payne

As long as the CurrencyManager.Count property > 0, you should be able to set the CurrencyManager.Position property to 0 (which would select the first item in the collection). I would expect incrementing -1 would give you 0...but did you check to make sure there was at least 1 item in the CurrencyManager first

Re: Windows Forms General System.IndexOutOfRangeException in Application.Run(new frmMain); line.


Finally I managed it.

I took me so long to reset the position value. Infact it wasn't fully my mistake. (ok %98 mine) I assumed refresh won't work so I tried to find a way to read new value of the position property but so surprisingly refresh fixed the problem.

Anyways thanks for your time, knowledge, advise and patience.