在dgv中选择一行

时间:2019-07-25 14:49:48

标签: vb.net datagridview

我使用datagridview进行了用户控件! 我知道如何选择一行,添加删除,这些东西。我需要使用事件选择已更改,但是在不使用用户控件的情况下进行交谈。 这里的问题是,由于我使用的是用户控件,因此无法选择datagridview并选择选择的事件选择已更改。有谁知道如何在dgv中选择用户控件中的行?

提前谢谢! 这是我在用户控件中拥有的

Private Sub grid1_SelectionChanged(sender As Object, e As EventArgs) Handles grid1.SelectionChanged
        Dim index As Integer
        Dim selectedrow As DataGridViewRow
        If Not grid1.CurrentRow Is Nothing Then
            Index = grid1.CurrentRow.Index
            selectedrow = grid1.Rows(Index)

        End If
    End Sub

在项目中,我试图从行和单元格中获取一些值。

Me.UcList1.tbName = "tblPeople"
        lblIDPeople.Text = Me.UcList1.grid1.selectedrow.Cells(0).Value.ToString

更新

Me.UcList1.tbName = "tblPeople"
        If Not Me.UcList1.grid1.CurrentRow Is Nothing Then
            lblIDPeople.Text = Me.UcList1.selectedrow.Cells(0).Value.ToSring
        End If

1 个答案:

答案 0 :(得分:0)

尝试一下。我测试了它的工作原理

  1. 在用户控件中,使代码看起来像这样

    Public Class MyControl
        Public selectedrow As DataGridViewRow
    
        Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
            selectedrow = DataGridView1.CurrentRow
        End Sub
    End Class
    
  2. 在主表单中,这样命名

    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If (Not MyControl1.selectedrow Is Nothing) Then
               MsgBox(MyControl1.selectedrow.Cells(1).Value)
               'lblIDPeople.Text = Me.MyControl1.Datagridview1.selectedrow.Cells(0).Value.ToString
            End If
        End Sub
    End Class
    

Output