动态生成的控件与之交互时运行的功能

时间:2018-09-18 10:40:19

标签: vb.net dynamic datagridview controls

双击datagridview单元格时,我无法使子程序运行。这是因为datagridview是通过编程方式创建的,而不是由设计人员创建的。我找到了一个将与该问题相关的帮助网站。

Public Class seattemplatecreator
    Dim alphabet() As Char = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()
    Private WithEvents dgv_flightTemplate As DataGridView

    'help from https://it.toolbox.com/question/event-for-dynamically-created-command-button-043008

    Public Sub init(ByVal dgv01 As DataGridView)
        dgv_flightTemplate = dgv01
    End Sub

    Private Sub dgv_flightTemplate_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgv_flightTemplate.CellMouseDoubleClick
        MsgBox("workwd")
    End Sub

    Private Sub btn_createflight_Click(sender As Object, e As EventArgs) Handles btn_createflight.Click
        'used https://social.msdn.microsoft.com/Forums/vstudio/en-US/e222f438-f060-4e61-ab28-523d02db91b2/how-to-programmatically-create-datagridview-with-empty-columns-and-rows?forum=vbgeneral
        'to help with this part for automatically generating the datagridview

        MsgBox(alphabet(0))

        Dim dgv_flightTemplate As New DataGridView
        Dim c As Integer = txb_columns.Text
        Dim r As Integer = txb_rows.Text

        For colcount As Integer = 0 To c - 1
            Dim nc As New DataGridViewTextBoxColumn
            nc.Name = "Seating Column"
            dgv_flightTemplate.Columns.Add(nc)
        Next
        dgv_flightTemplate.Rows.Add(r)

        For x = 0 To r - 1
            dgv_flightTemplate.Rows(x).HeaderCell.Value = alphabet(x).ToString
        Next

        Me.Controls.Add(dgv_flightTemplate)
        dgv_flightTemplate.Location = New Point(400, 400)
        dgv_flightTemplate.AllowUserToAddRows = False
        dgv_flightTemplate.AllowUserToDeleteRows = False
        dgv_flightTemplate.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders

        dgv_flightTemplate.AutoResizeRows()
        dgv_flightTemplate.AutoSize = True
    End Sub
End Class

https://it.toolbox.com/question/event-for-dynamically-created-command-button-043008

编辑:Olivier Jacot-Descombes的响应是完美的,只需要运行“ Init”子即可。     公共类seattemplatecreator

Dim alphabet() As Char = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()
Private WithEvents dgv_flightTemplate As DataGridView
'help from https://it.toolbox.com/question/event-for-dynamically-created-command-button-043008
Public Sub init(ByVal dgv01 As DataGridView)
    dgv_flightTemplate = dgv01
End Sub
Private Sub dgv_flightTemplate_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgv_flightTemplate.CellMouseDoubleClick
    MsgBox("workwd")
End Sub

Private Sub btn_createflight_Click(sender As Object, e As EventArgs) Handles btn_createflight.Click

    'used https://social.msdn.microsoft.com/Forums/vstudio/en-US/e222f438-f060-4e61-ab28-523d02db91b2/how-to-programmatically-create-datagridview-with-empty-columns-and-rows?forum=vbgeneral 
    'to help with this part for automatically generating the datagridview
    MsgBox(alphabet(0))
    Dim dgv_flightTemplate As New DataGridView
    Dim c As Integer = txb_columns.Text
    Dim r As Integer = txb_rows.Text
    For colcount As Integer = 0 To c - 1
        Dim nc As New DataGridViewTextBoxColumn
        nc.Name = "Seating Column"
        dgv_flightTemplate.Columns.Add(nc)
    Next
    dgv_flightTemplate.Rows.Add(r)
    For x = 0 To r - 1
        dgv_flightTemplate.Rows(x).HeaderCell.Value = alphabet(x).ToString

    Next
    Me.Controls.Add(dgv_flightTemplate)


    dgv_flightTemplate.Location = New Point(400, 400)
    dgv_flightTemplate.AllowUserToAddRows = False
    dgv_flightTemplate.AllowUserToDeleteRows = False
    dgv_flightTemplate.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders

    dgv_flightTemplate.AutoResizeRows()
    dgv_flightTemplate.AutoSize = True

    init(dgv_flightTemplate)






End Sub





End Class

任何帮助将不胜感激

谢谢泰恩

1 个答案:

答案 0 :(得分:0)

您没有呼叫Init。但是,您也可以直接将其分配给该字段

dgv_flightTemplate = New DataGridView 'Note: No Dim here.

但是,即使您动态创建列,也可以使用设计器添加网格。只需致电

dgv_flightTemplate.Columns.Clear()

在添加列之前。