【问题标题】:Adding multiple datasources to a datagridview vb.net将多个数据源添加到 datagridview vb.net
【发布时间】:2017-02-07 02:21:54
【问题描述】:

我有一个DataGridView,我正在从多个 Excel 文件中导入数据。但是每次我导入数据时,它都会覆盖以前的数据。如何让它在数据网格视图中将下一个 excel 文件添加到上一个文件的末尾?

If DataGridView1.RowCount > 0 Then
    MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\Work\4pc_test1.xlsx;Extended Properties=Excel 12.0;")

    'MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & fd.FileName & "';Extended Properties=Excel 8.0;")
    MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
    MyCommand.TableMappings.Add("Table", "Net-informations.com")
    DtSet = New System.Data.DataSet
    MyCommand.Fill(DtSet)

    'DataGridView1.DataSource = DtSet.Tables(0)

    Dim tab As DataTable = DtSet.Tables(0)
    DataGridView1.DataSource = tab

    MyConnection.Close()
Else

    'The below connection allows for the opening of .xls files and .xlsx. The one reamed out below doesnt open up .xls files. 
    MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\Work\4pc_test1.xlsx;Extended Properties=Excel 12.0;")

    'MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & fd.FileName & "';Extended Properties=Excel 8.0;")
    MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
    MyCommand.TableMappings.Add("Table", "Net-informations.com")
    DtSet = New System.Data.DataSet
    MyCommand.Fill(DtSet)
    DataGridView1.DataSource = DtSet.Tables(0)

    MyConnection.Close()
End If

【问题讨论】:

  • 一个 DGV 不能有多个数据源——它是单数的。是我,还是 If 和 Else 块都在做同样的事情,除了一个有一个额外的 DataTable 变量
  • 不是没有,但你问了16个问题,得到了18个答案;只接受了一个,只投票了一次。接受您认为有用的答案和点赞帖子可以帮助其他人找到好的答案。 Tour 解释了它。如果来源相同,可以追加到数据表中。

标签: .net excel vb.net winforms datagridview


【解决方案1】:

您可以简单地多次填充单个DataTable,这样就会将行添加到DataTable。例如:

Try
    Dim table = New DataTable()
    Dim connection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=D:\excel1.xlsx;" & _
        "Extended Properties=Excel 12.0;"
    Using adapter As New OleDbDataAdapter("select * from [Sheet1$]", connection)
        adapter.Fill(table)
    End Using
    connection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=D:\excel2.xlsx;" & _
        "Extended Properties=Excel 12.0;"
    Using adapter As New OleDbDataAdapter("select * from [Sheet1$]", connection)
        adapter.Fill(table)
    End Using
    Me.DataGridView1.DataSource = table
Catch ex As Exception
    MessageBox.Show(ex.ToString())
End Try

不同的excel文件中的列数可以不同,但​​如果有同名的列,那么这些列的数据应该是同一类型的。

【讨论】:

  • 我会敦促 SQL 拼出所需的列,以确保 foo 将与其他 foo 一起出现在 foo 列中
  • @Reza Ahaei,尽管您的答案导入了数据,但它不会将新记录堆叠在旧记录的末尾。例如,我的第一次导入是 100 条记录,第二次是 200 条,那么我的数据表中应该有 300 条记录。
  • @Chrisetiquette 您将拥有 300 条记录。
  • 不要把你的问题混在一起。本帖针对当前问题,成功导入2个excel文件。
  • @RezaAghaei 那我应该单独发一个帖子吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多