9

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
4

2 回答 2

21

要获取单元格值,您需要直接从DataGridView1usinge.RowIndexe.ColumnIndexproperties 中读取它。

例如:

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
于 2013-05-06T07:58:27.820 回答
0

I was having the same issue and this works excellently.

Private Sub DataGridView17_CellFormatting(sender As Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView17.CellFormatting  
  'Display complete contents in tooltip even though column display cuts off part of it.   
  DataGridView17.Rows(e.RowIndex).Cells(e.ColumnIndex).ToolTipText = DataGridView17.Rows(e.RowIndex).Cells(e.ColumnIndex).Value 
End Sub
于 2015-11-13T22:51:07.253 回答