2015-06-11 34 views
0

GD所有,设置栏的可见性增加的DataGridView的添加控件到窗体

我有一个非常简单的形式,增加了标签页上,有选择记录的循环之前(属性)。在添加的选项卡上,它插入一个DataGridView以显示每个选项卡标识符的记录选择。

为了做到这一点,我下面的代码创建:

For Each r As DataRow In tnkTable.Rows 

     Dim tmpTableAdapter As New RD_BWMDataSetTableAdapters.tblEventRegisterTableAdapter 
     Dim newTab As New TabPage() 
     Dim newGrid As New DataGridView() With {.Location = New Point(6, 6), .Width = 800} 
     Dim newBindingSource As New BindingSource() 
     Dim newDataview As DataView 

     newDataview = tmpTableAdapter.GetData.AsDataView 

     With newDataview 
      .Sort = "utcDateTime DESC" 
      .RowFilter = "IdTank = " & r("Id").ToString 
     End With 

     With newGrid 
      .Name = "dg" & r("tankShortCode").ToString 
      .DataSource = newDataview 
     End With 

     With newTab 
      .Name = r("tankShortCode").ToString 
      .Text = r("tankShortCode").ToString 
      .Controls.Add(newGrid) 
     End With 

     With Me.tabTankTable 

      .TabPages.Add(newTab) 

     End With 

     'End If 
    Next 

这基本上每插入标签页上正确的DataGridView施加到DataGridView的相关过滤。

然而,挑战在于我想隐藏每个datagridview的前3列。但是,当我尝试在DataGridView对象(即'newGrid')上做这件事时,它不允许我这样做,因为'newGrid'对象似乎没有任何列?

我试过几种途径,但一直没有能够产生预期的结果。

在我看来有两个选择:

  1. 找出为什么“newGrid”对象没有列,尽管有包含正确的数据(更新/刷新?)
  2. 捕获数据源添加控件并在添加实际控件后修改列。我也试过这个,但得到列索引错误,指示Control对象也没有列。

然而,当查看窗体时,所有的dgView都有相应的数据,并且都有列?

有什么建议吗?

的Martijn托伦

回答

2

由于列自动生成.DataSource设置后,它们将被添加。
添加DataBindingComplete事件处理程序,在那里你可以隐藏/删除列

With newGrid 
    .Name = "dg" & r("tankShortCode").ToString 
    .DataSource = newDataview 
    AddHandler .DataBindingComplete, AddressOf Me.DataGridView_DataBindingComplete 
End With 

创建事件处理程序

Private Sub DataGridView_DataBindingComplete(sender As Object, e AsDataGridViewBindingCompleteEventArgs) 
    Dim dgv As DataGridView = TryCast(sender, DataGridView) 
    If dgv Is Nothing Then Exit Sub 
    For Each column As DataGridViewColumn In dgv.Columns.Cast(Of DataGridViewColumn).Take(3) 
     column.Visible = false 
    Next 

End Sub 
+0

干杯法比奥, 作品的魅力,非常感谢! – mtholen