繁体   English   中英

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

[英]how to get data from selected row from datagridview

我有一个新问题,我有一个datagridview,试着看图片,我想在datagridview中存在的单元格点击,然后点击输入到textbox1的数据,任何人都知道怎么在哪里? 谢谢你的帮助

在此输入图像描述

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

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

要获取单元格值,您需要使用e.RowIndexe.ColumnIndex属性直接从DataGridView1读取它。

例如:

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

我有同样的问题,这非常有效。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM