我從excel導入3列到vb .net。然後,我想創建第四行或新的DataGridView,並在每行數據上追加一些列。例如我有excel文件:將數據從excel導入到vb .net
12/26/2016 9A Christmas Day
3/25/2016 9A Good Friday
3/28/2016 AS Easter Monday
12/26/2016 AS St. Stephen's Day
3/25/2016 AS Easter Monday
1/1/2016 AS New Year's Day
12/26/2016 BE Christmas
1/1/2016 BZ New Year's Day
2/9/2016 BZ Carnival
5/26/2016 BZ Corpus Christ
4/21/2016 BZ Tiradentes Day
12/27/2016 CA Boxing Day (obs)
10/10/2016 CA Thanksgiving
我想要的是,在第二列的內容後添加新列,其中日期相同。例如:
12/26/2016 9A Christmas Day 9A AS BE
3/25/2016 9A Good Friday 9A
3/28/2016 AS Easter Monday AS
12/26/2016 AS St. Stephen's Day 9A AS BE
3/25/2016 AS Easter Monday AS
1/1/2016 AS New Year's Day AS BZ
12/26/2016 BE Christmas 9A AS BE
1/1/2016 BZ New Year's Day AS BZ
2/9/2016 BZ Carnival BZ
5/26/2016 BZ Corpus Christ BZ
4/21/2016 BZ Tiradentes Day BZ
12/27/2016 CA Boxing Day (obs) CA
10/10/2016 CA Thanksgiving CA
具有相同日期的行在第四列中將具有相同的值。列名是(Date,CDR Code,Holiday)。數據是工作表的名稱。代碼中,我寫道:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyConnection As New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source='d:\test.xlsx';Extended Properties='Excel 12.0;HDR=YES'")
Dim MyCommand As New System.Data.OleDb.OleDbDataAdapter("select * from [Data$]", MyConnection)
Dim DtSet As New System.Data.DataSet
MyCommand.Fill(DtSet)
DataGridView1.DataSource = DtSet.Tables(0)
MyConnection.Close()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim MyConnection2 As New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source='d:\test.xlsx';Extended Properties='Excel 12.0;HDR=YES'")
Dim MyCommand2 As New System.Data.OleDb.OleDbDataAdapter("select [CDR Code] from [Data$] WHERE [Date]=[Date] ", MyConnection2)
Dim DtSet2 As New System.Data.DataSet
MyCommand2.Fill(DtSet2)
DataGridView2.DataSource = DtSet2.Tables(0)
MyConnection2.Close()
End Sub
End Class
您需要解決你的問題:「我從Excel導入3列VB.NET中然後我想創建一個第四行或新的DataGridView「 - 這沒有任何意義。您需要清楚 –
我在1個datagridview中插入3列。出於上述原因,我想添加第四行。如果無法在導入3個excel列的datagrid中附加第4行,則創建新的Datagrid,其中將顯示我想要的結果。 – Masyaf
「我插入3列....我想添加第四行」 - 這是令人困惑的。你需要開始思考更多的動態!你可以使用4個屬性('List(Of MyPoco)')的POCO對象列表作爲你的'DataSource',並從DB/Excel加載3個屬性(我希望你使用Microsoft.ACE.OleDB)。一旦你加載你的數據列表,你只需分配'.Datasource ='屬性 - WOW! –