VB.NET Winforms Select Row in DataGridView and get values

I usually work in ASP.NET.  I am trying to figure out how to select a row and get the value of the rows that I selected from the DataGridView on my WinForms.

Thanks
LVL 2
CipherISAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike EghtebasDatabase and Application DeveloperCommented:
This is a sample code from one of my projects. In this example, upon dbl-click on the dgv header customer id in Cells(0) is read. You can read Cells(1), Cells(2), etc as you need. I hope this example helps.

The following lines relate to your question but below it I have the complete code with other code lines:

Dim iRow As Int32 = e.RowIndex
' read CustomerID of the record Dbl-Clicked to pass it on to the edit form
intCustID = Convert.ToInt32(Me.TblCustomersDataGridView.Rows(iRow).Cells(0).Value)

Private Sub TblCustomersDataGridView_RowHeaderMouseDoubleClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles TblCustomersDataGridView.RowHeaderMouseDoubleClick

        Try

            ' upon dbl-click on RwoHeader, open edit form
            Dim intCustID As Int32
            Dim iRow As Int32 = e.RowIndex

            ' read CustomerID of the record Dbl-Clicked to pass it on to the edit form
            intCustID = Convert.ToInt32(Me.TblCustomersDataGridView.Rows(iRow).Cells(0).Value)

            ' save the row number of the record in question to
            ' use it to high light it after save or cancel
            intCustIDReturn = iRow

            ' instantiate a form object based on frmCustomerDetails
            Dim frmCustDetail As New frmCustomerDetails

            ' pass CustomerID of the record in question to the edit form
            frmCustDetail.intCustID = intCustID ' 

            ' show the edit form
            frmCustDetail.Show()

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

    End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CipherISAuthor Commented:
Thank You.  I changed it to Mouse Click.  This solved my problem.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.