我用 datagridview 制作了一个用户控件!我知道如何选择一行,添加删除,这些东西。我需要使用事件 selectionchanged,但不使用 usercontrol 进行交谈。这里的问题是,我无法选择 datagridview 并选择事件 selectionchanged,因为我使用的是用户控件。有谁知道如何在用户控件中的 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
试试这个。我测试了它及其工作
在用户控件中,使代码看起来像这样
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
在主窗体中,这样调用
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句