2016-07-16 144 views
0

我一直在尋找如何做到這一個小時,但所有我看到的都是C#中的答案..我想在Datagridview1中顯示tbl_scancheck以下的數據爲了(這些是列標題):Emp_No, DN_PickList, PickList_DT, DN_Thermal,Thermal_DT, TALLY如何將數據表的選定列綁定到datagridview

然而,tbl_scancheck是按此順序:DN_PickList, PickList_DT, DN_Thermal,Thermal_DT, TALLY, Emp_No

當前tbl_scancheck順序目前正在另一個datagridview的使用,因此我不能手動編輯MS Access。

這裏是我當前的代碼:

Dim dbprovider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = C:\Users\austin\Documents\Visual Studio 2015\Projects\ScanCheckSystem\ScanCheckSystem\bin\Debug\packerdb.accdb" 
Dim myConnection As OleDbConnection = New OleDbConnection(dbprovider) 
Dim da As OleDbDataAdapter 
Dim ds As DataSet 
Dim tables As DataTableCollection 
Dim source1 As New BindingSource 

Private Sub Packer_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
ds = New DataSet 
    tables = ds.Tables 
    da = New OleDbDataAdapter("Select * from [tbl_scancheck]", myConnection) 
    da.Fill(ds, "tbl_scancheck") 
    Dim view As New DataView(tables(0)) 
    source1.DataSource = view 
    DataGridView1.DataSource = view 
End Sub 
+0

在設計器中,按照您需要的順序在「DataGridView」中創建列。將'DataGridViewColumn'的'DataProperty'設置爲'DataTable'中列的相應名稱。並在設置'.DataSource'設置'DataGridView.AutoColumnGeneration = false'之前'Load'事件處理程序。 – Fabio

回答

3

在行,你分配你一個SELECT * FROM SQL命令通過OleDbDataAdapter -

da = New OleDbDataAdapter("Select * from [tbl_scancheck]", myConnection) 

既然你知道你想要的列的名稱以及您想要它們的順序,如果您修改SQL以顯式命名列,它可能會工作嗎?示例 -

da = New OleDbDataAdapter("SELECT Emp_No, DN_PickList, PickList_DT, DN_Thermal,Thermal_DT, [TALLY] FROM [tbl_scancheck]", myConnection) 
+0

我目前正在計算法比奧的答案,並看到你的..中間試了它,並工作。感謝您的簡單解決方案。 – austin

+0

不客氣!樂意效勞 – LogicalFlaps

相關問題