Results 1 to 4 of 4

Thread: DataGridView Error On Column Resize

  1. #1
    curlydog is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 3.5
    Join Date
    Jul 2010
    Posts
    50
    Reputation
    85

    DataGridView Error On Column Resize

    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
    Code:
    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

  2. #2
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,085
    Reputation
    2803
    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.
    How to format posts with code blocks etc - present the problem/post properly

    Visual Studio Community 2017

  3. #3
    curlydog is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 3.5
    Join Date
    Jul 2010
    Posts
    50
    Reputation
    85
    My mistake John, there are 8 columns in my dataGridView, not 6.

  4. #4
    curlydog is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 3.5
    Join Date
    Jul 2010
    Posts
    50
    Reputation
    85
    Quote Originally Posted by JohnH View Post
    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •