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 (i.e. '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:
- Find out why the 'newGrid' object does not have columns despite having a datasource that contains correct data (update/refresh ??)
- 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
Create Event handler