Dynamic Cell单击DataGridView VB.NET

时间:2016-01-19 14:15:46

标签: vb.net datagridview

我有一个应用程序,它被分成了标签。最多4个选项卡可以使用相同的DataGridView,但具有不同的数据。

我动态创建标签和数据网格视图,它们被命名为“Entitlements1”,“Entitlements2”等。

我为DataGridView添加了一个Cell Double Click事件处理程序,它适用于两个选项卡上的两个DataGridViews,但是如何确定它为哪个DataGridView触发了事件?

我的代码:

Public Shared Sub EntitlementsDGV(ByVal DriverNo As Integer)

        Dim entdgv As New List(Of DataTable)
        entdgv.Add(New DataTable)
        entdgv.Add(New DataTable)
        entdgv.Add(New DataTable)
        entdgv.Add(New DataTable)
        entdgv.Add(New DataTable)

        'Find Driver Tab Control
        Dim DriverTabCtrl = Functions.FindControl(Results, "DriverTab" & DriverNo)
        'Find GroupBox Control
        Dim GroupBoxCtrl = Functions.FindControl(DriverTabCtrl, "EntitlementGrpBx" & DriverNo)
        'Find DataGridView
        Dim DGVCtrl As DataGridView = Functions.FindControl(GroupBoxCtrl, "Entitlements" & DriverNo)

        entdgv(DriverNo) = Results.EntitlementsTbl.Clone
        Dim result() As DataRow = Results.EntitlementsTbl.Select("Driver = " & DriverNo & "")
        For Each row As DataRow In result
            entdgv(DriverNo).Rows.Add(row(0), row(1), row(2), row(3), row(4), row(5))
        Next

        DGVCtrl.DataSource = entdgv(DriverNo)
        DGVCtrl.Columns(DGVCtrl.Columns.Count - 1).Visible = False

        AddHandler DGVCtrl.CellDoubleClick, AddressOf EntitlementDblClick

    End Sub

    Public Shared Sub EntitlementDblClick(sender As Object, e As DataGridViewCellEventArgs)
        MsgBox("Entitlement Box Double Click")
    End Sub

这是我动态添加DataGridView

的代码
Dim NewDGV As New DataGridView
            NewDGV.Name = "Entitlements" & DriverNo
            NewDGV.Location = New Point(1, 1)
            DGVSetup(NewDGV)

            NewGrpBx.Controls.Add(NewDGV)

1 个答案:

答案 0 :(得分:2)

Public Shared Sub EntitlementDblClick(sender As Object, e As DataGridViewCellEventArgs)

        MsgBox("Entitlement Box Double Click")

        Dim dgv As DataGridView = DirectCast(sender, DataGridView)

    End Sub

dgv将是触发事件的datagridview