.NET datagridview FirstDisplayedScrollingColumnIndex throwing exception

By rsimlote ·
Hi guys,
I am developing a winform application that contains a couple of datagridview a tab control.
One of the datagridview has some frozen columns and then a bunch of dynamic columns created after that. These dynamic columns created based on number of years ( 2 columns per year).
I want the datagrid to scroll directly to the current year so that the older years are hidden and that users don't have to scroll the grid to see the current year.
Everything is working okay, the auto-scrolling is working fine, except when I close the form, I get the following JIT-exception:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Earlier I was not able to figure out the problem, but after some research, I found out that the following line of code is giving the error.

Me.MyDatagridlayedScrollingColumnIndex = FirstVisibleColumnIndex

I double checked that my FirstVisibleColumnIndex is greater than my frozen columns and less then the total number of columns.

When I commented this line, everything is working fine, I am not getting the JIT exception.

Please suggest what could be the possible problem and what should I do to resolve it? Thank you.

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -

Why is close calling that?

by Tony Hopkinson In reply to .NET datagridview FirstDi ...

Stopping it as in setting a flag that you are closing is a quick fix, but this to me would indicate that something is quite wrong with the lifetimes of your code.

No real way to tell from here, but, if you don't have a formclose or dispose handler, chuck one in, stick a debug in it and trace it through.

For instance you might be trying to do something with that property, while .Net is or has got rid of some of it.

Collapse -

Close does not run this line of code

by rsimlote In reply to Why is close calling that ...

I have a function that handles the form closing event. I debug the code but the program control does not call the function that has this line of code. Something is telling me that this line leaves a trail that is causing the problem....

Collapse -

Now I'm confused as well!

by Tony Hopkinson In reply to Close does not run this l ...

you said you got the exception when you closed the form.
And you said you tracked it to that line of code....

You also said if you commented it out the error went away.

Unless you have got confused A + B equals this code is running somewhere during the process of closing the form.

There are lots of ways this could be happening, even more if your grid is dataaware.

If it is only happening on close, then close is doing something with the number of columns in your grid such as disposing of all the columns objects, which then sets column count to 0, which then means setting firstVisble will raise an exception.

If what you told me is right, it's got to be that sort of scenario, or some small blue aliens are crawling around in your pc, switching ones and zeros as a wee prank...

Collapse -

using FirstDisplayedCell instead

by rsimlote In reply to Now I'm confused as well!

Nothing is crawling around my PC, I also believe that it has something to do with the disposed functionality when using FirstDisplayedScrollingColumnIndex.

To work around this problem, I modified my code by setting the FirstDisplayedCell to the cell at the desiered column index in row 0.

So instead of using
Me.MyDatagrid.FirstDisplayedScrollingColumnIndex = FirstVisibleColumnIndex

I am using

Me.MyDatagrid.FirstDisplayedCell = Me.MyDatagrid.Rows(0).Cells(FirstVisibleColumnIndex)

this is working fine for me and I am not getting any JIT error.

Collapse -

Definitely suggests the old code was

by Tony Hopkinson In reply to using FirstDisplayedCell ...

allowing you access a property while the object was in an undefined state, can't say I'm surprised MS can leave quite a lot to be desired on the front. I've have problems in this area before.

Glad to help even if it was only trying to explain it to me, organised the old thought processes.

Related Discussions

Related Forums