Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get data from selected row from datagridview

i have a new problem, I have a datagridview, try to see the picture, I want when cells that exist in the datagridview on click, then click on the data entered into textbox1, anyone know how where how? thanks for helping me

enter image description here

I was tried like below, but its not work

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)


            'TextBox1.Text = Me.DataGridView1.Rows(Me.DataGridView1.row).Cells(1).Value
        End If
    End Sub
like image 860
Vinra Gunanta Pandia Avatar asked May 06 '13 06:05

Vinra Gunanta Pandia


1 Answers

To get the cell value, you need to read it directly from DataGridView1 using e.RowIndex and e.ColumnIndex properties.

Eg:

Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
   Dim value As Object = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value

   If IsDBNull(value) Then 
      TextBox1.Text = "" ' blank if dbnull values
   Else
      TextBox1.Text = CType(value, String)
   End If
End Sub
like image 178
ajakblackgoat Avatar answered Oct 12 '22 23:10

ajakblackgoat