vb.net 如何从datagridview的选定行中获取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16393408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get data from selected row from datagridview
提问by Vinra Gunanta Pandia
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
我有一个新问题,我有一个datagridview,尝试查看图片,我想要点击datagridview中存在的单元格,然后点击输入textbox1的数据,有谁知道如何如何?谢谢你帮助我
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
回答by ajakblackgoat
To get the cell value, you need to read it directly from DataGridView1
using e.RowIndex
and e.ColumnIndex
properties.
要获取单元格值,您需要直接从DataGridView1
usinge.RowIndex
和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
回答by ThomasN
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