DataGridView Error On Column Resize

curlydog

Well-known member
Joined
Jul 5, 2010
Messages
50
Programming Experience
Beginner
Folks,
I'm new to this game and struggling to figure out why this error is occurring and how I should solve it.

At first the problem seemed intermittent, but I've now figured out how to replicate it over and over. I have a datagridview control with six columns, two if which are datagridviewLinkcolumns. If the user clicks on one of the links in one of these columns, the event is handled using CellContentClick.

If I immediately resize one of the link columns, immediately after it's contents are clicked, I get an error. The error is generated in my CellContentClick routine and relates to the line
VB.NET:
Dim threadID as String = dgvPosts.Rows(e.RowIndex).Cells(6).Value


The error is an out of range exception and states the index was out of range must be non-negative and less than the size of the collection. Strange (to me) that the error is actually happening when the column is being resized and not when a cell contents are clicked.

Thanks
 
If you have 6 columns, then Cells indexes are 1-5. Maybe you have more columns, but some are hidden? If not Cells(6) is out of range, but will be the same the whole time and not in a particular case.
Otherwise, could e.RowIndex be -1 by any chance? This will happen sometimes when you click a column header.
When exception is thrown you can inspect with debugger what is wrong, for example hover RowIndex and you will see its current value.
 
Otherwise, could e.RowIndex be -1 by any chance? This will happen sometimes when you click a column header.
When exception is thrown you can inspect with debugger what is wrong, for example hover RowIndex and you will see its current value.
You're right John, e.RowIndex is -1 in debugger.
I solved it by putting my code within an If statement were e.RowIndex > -1 and it seems to be working fine.

Thanks for your help
 

Latest posts

Back
Top