如何从datagridview中的选定行获取数据

时间:2013-05-06 06:46:58

标签: vb.net datagridview

我有一个新问题,我有一个datagridview,尝试查看图片,我想在datagridview中存在单元格时点击,然后点击输入到textbox1的数据, 谁知道怎么怎么样? 谢谢你的帮助

enter image description here

我的尝试如下,但它不起作用

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

2 个答案:

答案 0 :(得分:20)

要获取单元格值,您需要使用DataGridView1e.RowIndex属性直接从e.ColumnIndex阅读。

例如:

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

答案 1 :(得分:0)

我遇到了同样的问题而且效果很好。

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