将多个Datagridviews加载到不同的Tabpages

时间:2015-08-20 13:35:52

标签: vb.net datagridview tabpage

概述:我正在创建一个允许用户创建包含多个列和页面的寄存器的应用程序。页面数是动态的(用户可以根据需要添加任意数量的页面)。每个标签页都有一个显示列的网格视图。列由用户定义。 当他们创建标签页或新列时,它会将信息发布到XML文件。在打开应用程序时,它在XML中读取以显示工作表(带有名称)和列(带有名称)。

问题:当我使用gridview为一个标签页运行下面的代码时,一切正常(我可以在标签页中看到网格视图)。当我运行多个标签页的代码时,我可以看到标签页,但是gridview没有显示。

    Private Sub UF_DEF_COL_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    Dim doc As New XmlDocument()
    doc.Load("C:\Users\geoff.warrington\Documents\Visual Studio 2010\Projects\Project Register\Project Register\Tab_List.xml")
    Dim root As XmlElement = doc.DocumentElement
    Dim node_list As XmlNodeList = root.GetElementsByTagName("Tab")

    Me.SHEET_DEF.TabPages.Remove(Register)
    Dim x As Integer
    Dim Y As Integer
    Dim TB = New TabPage
    Dim Col_Data_Grid As New DataGridView

    For x = 0 To node_list.Count - 2
        Dim No_Cols As Integer = node_list(x).ChildNodes.Count - 1
        Me.SHEET_DEF.TabPages.Add(TB)
        With TB
            .Location = New System.Drawing.Point(4, 22)
            .Name = node_list(x).FirstChild.InnerText
            .Padding = New System.Windows.Forms.Padding(3)
            .Size = New System.Drawing.Size(805, 172)
            .TabIndex = x + 1
            .Text = node_list(x).FirstChild.InnerText
            .UseVisualStyleBackColor = True
        End With

        Col_Data_Grid = New DataGridView() With { _
        .Name = node_list(x).FirstChild.InnerText,
        .ColumnCount = node_list(x).ChildNodes.Count - 1,
        .Location = New Point(8, 8),
        .Size = New Size(1000, 25),
        .AutoSizeRowsMode = _
           DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders,
        .ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single,
        .CellBorderStyle = DataGridViewCellBorderStyle.Single,
        .GridColor = Color.Black,
        .RowHeadersVisible = False,
        .SelectionMode = DataGridViewSelectionMode.FullRowSelect,
        .MultiSelect = False,
        .Dock = DockStyle.Fill,
        .Visible = True}

        For Y = 1 To No_Cols
            Col_Data_Grid.Columns(Y - 1).Name = node_list(x).ChildNodes(Y).InnerText
        Next

        SHEET_DEF.TabPages(x).Controls.Add(Col_Data_Grid)

    Next

End Sub
Friend WithEvents Register As System.Windows.Forms.TabPage
Friend WithEvents Col_Sel As System.Windows.Forms.DataGridView
Friend WithEvents ID As System.Windows.Forms.DataGridViewTextBoxColumn
Friend WithEvents Col_Data_Grid As DataGridView

编辑新代码如下所示。这仍然有同样的问题,但我觉得它更接近。其中" node_list.count - 2"返回一个tabpage,datagridview完全可见。当我将其更改为" node_list.count - 1"我可以看到两个tabpages,但两个tabpage都没有datagridview。我不明白为什么它适用于一个但不超过一个。

    Private Sub UF_DEF_COL_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    Dim doc As New XmlDocument()
    doc.Load("C:\Users\geoff.warrington\Documents\Visual Studio 2010\Projects\Project Register\Project Register\Tab_List.xml")
    Dim root As XmlElement = doc.DocumentElement
    Dim node_list As XmlNodeList = root.GetElementsByTagName("Tab")

    Me.SHEET_DEF.TabPages.Remove(Register)
    Dim x As Integer
    Dim Y As Integer
    Dim TB As New TabPage

    For x = 0 To node_list.Count - 2
        Dim No_Cols As Integer = node_list(x).ChildNodes.Count - 1
        Me.SHEET_DEF.TabPages.Add(TB)
        With TB
            .Location = New System.Drawing.Point(4, 22)
            .Name = node_list(x).FirstChild.InnerText
            .Padding = New System.Windows.Forms.Padding(3)
            .Size = New System.Drawing.Size(805, 172)
            .TabIndex = x + 1
            .Text = node_list(x).FirstChild.InnerText
            .UseVisualStyleBackColor = True
        End With

        Dim Col_Data_Grid = New DataGridView()

        With Col_Data_Grid
            .Name = node_list(x).FirstChild.InnerText & "DGV"
            .ColumnCount = node_list(x).ChildNodes.Count - 1
            .Location = New Point(8, 8)
            .Size = New Size(1000, 25)
            .AutoSizeRowsMode = _
               DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
            .AutoSizeColumnsMode = _
                DataGridViewAutoSizeColumnMode.DisplayedCells
            .ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single
            .CellBorderStyle = DataGridViewCellBorderStyle.Single
            .GridColor = Color.Black
            .RowHeadersVisible = False
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            .MultiSelect = False
            .Dock = DockStyle.Fill
            .Visible = True
        End With

        For Y = 1 To No_Cols
            Col_Data_Grid.Columns(Y - 1).Name = node_list(x).ChildNodes(Y).InnerText
        Next

        Col_Data_Grid.Parent = Me.SHEET_DEF.TabPages(x)
        Me.SHEET_DEF.TabPages(x).Controls.Add(Me.Col_Data_Grid)

    Next

End Sub
Friend WithEvents Register As System.Windows.Forms.TabPage
Friend WithEvents Col_Sel As System.Windows.Forms.DataGridView
Friend WithEvents ID As System.Windows.Forms.DataGridViewTextBoxColumn
Friend WithEvents Col_Data_Grid As System.Windows.Forms.DataGridView
End Class

1 个答案:

答案 0 :(得分:0)

如评论中所述,解决方案是将= new标签页移动到循环区域并添加括号。

http://www.aspforums.net/Threads/293990/Unable-to-load-datagridview-to-more-than-1-tabpage/?p=1#Replies