Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting the error when I select(click) on the row I want to get the customer number from. The Datagridview has over 200 rows and 9 columns. I want the contents of the second column(column 1) that shows on the Datagridview. The Datagridview columns have been reordered from the data file.

I am clicking on the 8th row so I can't be out of range for the row and I am retriving column(cell) 1, that should not be out of range with 9 columns.

I have used this same code in another form and it works.

What am I missing??

What I have tried:

Private AssgnCusNo As Integer = 0

Private Sub DataGridView2_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView2.CellContentClick
        Try
            AssgnCusNo = CInt(DataGridView2.SelectedRows(0).Cells(1).Value.ToString())
           
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
            errornumber = 272 'Assigned error number for logging & debugging.
            clsLogFile = New LogFile(ex.Message, errornumber)
        End Try

    End Sub
Posted
Updated 19-Feb-20 21:01pm

1 solution

It may be because you are using the SelectedRows collection; is there any chance that clicking on a cell does not automatically select the corresponding row?

Why not using the event arguments for that?
VB
AssgnCusNo = CInt(DataGridView2.Rows(e.RowIndex).Cells(1).Value.ToString())
 
Share this answer
 
Comments
bgcwaterman 20-Feb-20 15:29pm    
That did it.
Thanks...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900