Results 1 to 13 of 13

Thread: Getting error when click on DataGridView Header Column...

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    326

    Getting error when click on DataGridView Header Column...

    The following error pops up when I click on DataGridView Header Column.

    {"Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index"}

    What does it mean and how to avoid this error?

    Code:
    Private Sub DataGridView1_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
            'Assign values to textboxes from selected/higlighted row
            'e.index is the index value of current selected row
            'cell(1) for instance gets the value of the cell(row,column) of the current row selected
            txtEMPID.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString
            txtEMPNAME.Text = DataGridView1.Rows(e.RowIndex).Cells(1).Value.ToString
            txtEMPPROFESSION.Text = DataGridView1.Rows(e.RowIndex).Cells(2).Value.ToString
    End Sub

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Getting error when click on DataGridView Header Column...

    Which line does it give you the error on? What is happening is that somewhere when you set the Text of the various TextBox controls is that you're trying to access a Row or Cell that does not exist.

    We can immediately rule out that it's the Row causing the issue because you're using the DataGridViewCellMouseEventArgs to get the current row. This tells us that it's the Cell that is causing the problem. Whichever line is causing you the problems is telling you that the column at clicked row does not exist.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    326

    Re: Getting error when click on DataGridView Header Column...

    This the line which throws an error:
    Code:
    txtEMPID.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString

  4. #4
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    404

    Re: Getting error when click on DataGridView Header Column...

    You need to validate that the users selection is greater than 0 you carnt use that function on row headers as they have different accessible properties add an if statement in to check e.rowindex value is greater than 0 before calling the above and it won't throw an error

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Getting error when click on DataGridView Header Column...

    Then the column 0 does not exist. What you need to do is setup a breakpoint on that line and check the following:
    1. What the value of e.RowIndex is and compare that to the DataGridView1.Rows.Count value
    2. What the value of e.Columns.Count is
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    326

    Re: Getting error when click on DataGridView Header Column...

    e.RowIndex is -1. But how to check DataGridView1.Rows.Count value & e.Columns.Count?

  7. #7
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    404

    Re: Getting error when click on DataGridView Header Column...

    It would be -1 as that's the index for column headers which is why you need to validate that the e.rowindex is greater than or equal to 0 before executing that line of code

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Getting error when click on DataGridView Header Column...

    Then you're receiving the error because there is no such thing as a row at index number -1. So this brings up my next question, which row are you wanting to set the Text to?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    326

    Re: Getting error when click on DataGridView Header Column...

    Actually, I want to sort the column by clicking on it.

  10. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Getting error when click on DataGridView Header Column...

    Quote Originally Posted by VS2013 View Post
    Actually, I want to sort the column by clicking on it.
    That is the default behavior of a DataGridView which means that there is no coding needed on your end. Why were you setting TextBox text if you wanted to sort a DataGridView?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    326

    Re: Getting error when click on DataGridView Header Column...

    The code in my post#1 is to show data of a cell in related textboxes. And when I click on any of the cell in the Datagridview, it shows correctly in the textboxes. But when I click on the DGV Header column to sort it, that error pops-up.

    Thanks.

  12. #12
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    404

    Re: Getting error when click on DataGridView Header Column...

    As Previously stated, on the event validate that the selected row id is more than or equal to 0

    Code:
    If e.RowIndex >= 0 Then
    
    ' Do TextBox Population Stuff
    
    End If

  13. #13
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Getting error when click on DataGridView Header Column...

    I haven't used the DGV in a while, but know you can get into trouble with cascading events...in any case, checking the row index is probably the way to go, but the reason is that clicking the header cell may very well raise the CellMouseClick event.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

Posting Permissions

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



Click Here to Expand Forum to Full Width