简体   繁体   中英

Can't Refresh DataGridView on TabControl Page When There are More Columns Added to DataGridView

There is a DataGridView spawned dynamically and a TabControl is assigned as a parent. The DataGridView is populated with random numbers. While there are no problems with headers or cell values, I can't seem to be able to resize the DataGridView on the tab control page when more columns are added dynamically.

Is there an issue with the Parent scoping for the TabControl, that prevents regeneration and display of more columns on the TabControl, once everything is refreshed?

Dim datagridview1 As New DataGridView
Datagridview1.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders)

Dim dgvColumnHeaderStyle As New DataGridViewCellStyle()
dgvColumnHeaderStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
Datagridview1.ColumnHeadersDefaultCellStyle = dgvColumnHeaderStyle
Datagridview1.AllowUserToAddRows = False
Datagridview1.ScrollBars = ScrollBars.Both
DataGridView1.Font = New System.Drawing.Font("Lucida Sans Typewriter", 8)


For j As Integer = 1 To UBound(columnheaders)
  DataGridView1.Columns.Add(columnheaders(j), columnheaders(j))
Next
For j As Integer = 1 To UBound(columnheaders)
  DataGridView1.Columns(columnheaders(j)).HeaderText = columnheaders(j)
Next
For i As Integer = 0 To UBound(rowheaders) - 1
  Dim n As Integer = DataGridView1.Rows.Add()
  For j = 0 To UBound(columnheaders) - 1
    DataGridView1.Rows.Item(n).Cells(j).Value = Rnd()
    If i Mod 2 = 0 Then DataGridView1.Rows.Item(i).Cells(j).Style.BackColor = Color.White
    If i Mod 2 <> 0 Then DataGridView1.Rows.Item(i).Cells(j).Style.BackColor = Color.AliceBlue
  Next
Next

Datagridview1.Visible = True
Datagridview1.Height = Me.Height - 100
Datagridview1.Width = Me.Width - TabControl2.Left - 100
Datagridview1.Parent = TabControl2.TabPages(1)
TabControl2.TabPages(1).Refresh()
TabControl2.Refresh()
Datagridview1.Refresh()

RESOLVED: It worked out that any time you programatically add a datagridview to a tabcontrol, if you update (recreate) the datagridview and want to see the new datagridviw on the same tabcontrol page, you have to clear the controls on the tab page. Following gives an example for using a TabControl1 control and page 0, ie, the first tab:

Me.TabControl1.SelectedTab = TabControl2.TabPages.Item(0)
Me.TabControl1.TabPages(0).Controls.Clear()
Me.TabControl1.TabPages(0).Controls.Add(datagridview1)

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