如何在datagridview选中行doubleclick事件的变量中获取值

时间:2012-08-23 07:32:53

标签: variables datagridview vb.net-2010 double-click selectedvalue

我有这个数据网格视图:

enter image description here

现在,当双击第一行时,我希望将所有数据保存在变量中,例如字段name应该包含在变量名name中,而First Name应该来自另一个可变名fname等等。

我一直在尝试使用DataGridView1.CellContentDoubleClick事件获取值,使用名为SelectedCells的属性,但是intellisense没有向我提供有关如何获取该特定列中每列的值的任何信息选定的行。

2 个答案:

答案 0 :(得分:4)

要直接从datagridview获取数据,您可以执行以下操作:

Dim fName As String = String.Empty
Dim sName As String = String.Empty

Private Sub DataGridView1_CellContentDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentDoubleClick
    If DataGridView1.SelectedRows.Count <> 0 Then
        Dim row As DataGridViewRow = DataGridView1.SelectedRows(0)
        fName = row.Cells("fNameColumnName").Value
        sName = row.Cells("sNameColumnName").Value
    End If
End Sub

然而,这不会与设置为true的multiselect属性一起使用!

答案 1 :(得分:1)

最后我得到的答案对我有用,所以在这里分享:

Private Sub DataGridView1_CellContentClick_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentDoubleClick
        frm_dashboard.lbl_pnum_text.Text = DataGridView1.SelectedRows(0).Cells(0).Value
        frm_dashboard.lbl_pname_text.Text = DataGridView1.SelectedRows(0).Cells(2).Value + " " + DataGridView1.SelectedRows(0).Cells(1).Value
        frm_dashboard.lbl_paddress_text.Text = DataGridView1.SelectedRows(0).Cells(4).Value
        frm_dashboard.Enabled = True
        Me.Hide()
    End Sub