简体   繁体   中英

Set column visibility (properties) of added datagridview prior to adding the control to the form

GD All,

I've got a loop for a fairly simple form that adds tab pages for a selection of records. On the added tabs it inserts a DataGridView to display the selection of the records per tab identifier.

In order to do this I've created below code:

    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

This essentially insert a correct DataGridView on every tab page with the the relevant filter applied to the DataGridView.

The challenge however lies in the fact that I would like to hide the first 3 columns of each datagridview. But when I try to do this on the DataGridView object (ie 'newGrid') it does not allow me to do so as the 'newGrid' object does not appear to have any columns ?

I've tried several pathways but haven't been able to produce desired result.

In my opinion there are two options:

  1. Find out why the 'newGrid' object does not have columns despite having a datasource that contains correct data (update/refresh ??)
  2. Capture the added control and amend columns after the actual Control has been added. I've tried this as well but get an column index error, indicating that the Control object does not have columns either.

When viewing the form however, all dgViews have the appropriate data in them and all have columns ?

Any suggestions ?

Martijn Tholen

Because columns are autogenerated they will be added after .DataSource is set.
Add DataBindingComplete event handler where you can hide/remove columns

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

Create Event handler

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM