我有TabControl和1个TabPages并包含DataGridView。 我使用代码:
从TabPages1创建TabPages2 Dim Frm As New TabFormPage
Dim MoreTabs As TabPage = Frm.TabPage1
Dim i As Integer
For i = 1 To TabControl1.TabPages.Count
MoreTabs.Text = "TabPage" & i + 1
Next i
TabControl1.Controls.Add(MoreTabs)
Frm.Dispose()
TabPages2控件与TabPages1相同
如果我在TabPages1中使用BackgroundWorker填充DataGridView,它就可以了。但是如果我在不同来源的每个新TabPages(例如TabPages2)中填充DataGridView,它就无法工作(TabPages2中的DataGridView /每个New TabPages都是空的/什么都没有)。它总是填充Tabpages1 DataGridView
如何使用相同的控件填充每个New TabPages中的每个DataGridView? 这是我填写DataGridview的代码:
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FPathTab + ";Extended Properties=Excel 12.0;")
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [" & CSheetTab1 & "]'", MyConnection)
DtSet = New System.Data.DataSet
DtSet.Clear()
MyCommand.Fill(DtSet)
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
DataGridView1.DataSource = DtSet.Tables(0)
dv = DtSet.Tables(0).DefaultView
MaxRecords = DtSet.Tables(0).Rows.Count
MyConnection.Close()
DataGridView1.Columns("F20").Visible = False
DataGridView1.Columns("F21").Visible = False
DataGridView1.Columns("F22").Visible = False
End Sub
感谢您的帮助