Help with retreiving data from DatagridView using VB.NET

Posted on 2013-09-12
Medium Priority
Last Modified: 2013-10-01

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.

Question by:vcharles
  • 2
LVL 11

Expert Comment

ID: 39486339
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

Author Comment

ID: 39486997
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


LVL 18

Accepted Solution

John (Yiannis) Toutountzoglou earned 2000 total points
ID: 39496121
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
        End If
    End Sub

Open in new window


Author Closing Comment

ID: 39538365
Thank You!

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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.

Join & Write a Comment

Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
The PowerShell Core 6.0 of .NET release is just the beginning. The upcoming PowerShell Core 6.1 would have artificial intelligence and internet of things capabilities. So many things to look forward to in the upcoming release.
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…
In the video, one can understand the process of resizing images in single or bulk. Kernel Bulk Image Resizer is an easy to use tool for resizing large number of images. One can add and resize multiple images with this tool in single go. The video sh…

619 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question