如何将数据表的选定列绑定到datagridview

时间:2016-07-16 08:39:08

标签: vb.net winforms ms-access data-binding datagridview

我现在一直在寻找如何做一个小时,但我看到的只是C#中的答案..我想按照以下顺序在Datagridview1 tbl_scancheck显示数据(这些是列标题): Emp_No, DN_PickList, PickList_DT, DN_Thermal,Thermal_DT, TALLY

但是,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

1 个答案:

答案 0 :(得分:3)

在您分配OleDbDataAdapter的行中,您传递SELECT * FROM SQL命令 -

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)