面向TableLayoutPanel里面的VB net DataGridView

时间:2015-12-01 19:46:29

标签: vb.net datagridview tablelayoutpanel ctype

我正在尝试访问Panel内的DataGridView,它位于TableLayoutPanel内,位于 Form5 Form5 。我尝试了以下方法:

    ' this code from Form5
    Dim testPanel As Panel = CType(Form3.TableLayoutPanel1.Controls("Panel1"), Panel)
    Dim testdgv As DataGridView = CType(testPanel.Controls("topics_and_schedule_DGview"), DataGridView)
    ' instead of CType I tried DirectCast and TryCast

    MsgBox(testdgv.Parent.Name)  --> Display "Panel1"
    MsgBox(testdgv.Name)   --> Display "topics_and_schedule_DGView"
    MsgBox(testdgv.Columns.Count) --> Display 0 (should be 2)

    MsgBox(Form3.topics_and_schedule_DGview.Parent.Name) --> Display "Panel1"
    MsgBox(Form3.topics_and_schedule_DGview.Columns.Count) --> Display 0 (should be 2)

当我写下以下内容时:

Form3.TableLayoutPanel1.Panel1.topics_and_schedule_DGview

在设计视图中抱怨说“Panel1不是TableLayoutPanel的成员

当我运行以下内容时:

Dim cControl As Control
    For Each cControl In testPanel.Controls
        If cControl.Name = "topics_and_schedule_DGview" Then
            Dim ss As DataGridView = cControl
            MsgBox(ss.Columns.Count)   ---> Display 0 
        End If
        TextBox1.Text = TextBox1.Text + " -- " & cControl.Name

    Next cControl

我能够看到所需的DataGridView,但随着列数的增加而变为零!!

知道出了什么问题吗?

0 个答案:

没有答案