Help with retreiving data from DatagridView using VB.NET

Hi,

My DataGridView contains one columm, when I click on a row how do I pass its text to a string variable? I am using VS 2010 with VB.NET.

Victor
vcharlesAsked:
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.

SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
hi... you can get this value through the below code

Protected Sub DataGrid1_ItemCommand(source As Object, e As DataGridCommandEventArgs)
      Select Case e.CommandName
            Case "Select Item"
                  Dim selectedItem As DataGridItem = e.Item
                  Dim firstCellText As String = selectedItem.Cells(0).Text
                  Dim secondCellText As String = selectedItem.Cells(1).Text
                  Exit Select
      End Select
End Sub

And also with this code

Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        If Me.DataGridView1.RowCount > 0 Then

            TextBox1.Text = Convert.ToString(Me.DataGridView1.SelectedRows)
           
        End If
    End Sub
0
vcharlesAuthor Commented:
Hi,
Is your solution for a DataGridView for a Windows applications?
The first part of the code code not be used because the Grid does not have an ItemCommand property.

and the second part of the code returns value: System.Windows.Forms.DataGridViewSelectedRowSelected


Thanks,

Victor
0
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Hi Victor
when I click on a row how do I pass its text to a string variable?

You can use
RowHeaderMouseClick or RowHeaderMouseDoubleClick events ..
Something like
 Dim SendString As String
    Private Sub DataGridView1_RowHeaderMouseClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.RowHeaderMouseClick
        If e.Button = Windows.Forms.MouseButtons.Left Then
            SendString = Me.DataGridView1.Rows(e.RowIndex).Cells(0).Value
            MessageBox.Show(SendString)
        End If
    End Sub

Open in new window


Yiannis
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
vcharlesAuthor Commented:
Thank You!
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
.NET Programming

From novice to tech pro — start learning today.