austin austin - 4 months ago 28
Vb.net Question

How to bind selected columns of a datatable to a datagridview

I've been searching on how to do this for an hour now but all I see are answers in C#.. I want to display data in

Datagridview1
from
tbl_scancheck
in the following order (these are the column headers):
Emp_No, DN_PickList, PickList_DT, DN_Thermal,Thermal_DT, TALLY


However,
tbl_scancheck
is in this order:
DN_PickList, PickList_DT, DN_Thermal,Thermal_DT, TALLY, Emp_No


The current tbl_scancheck order is currently being used in another datagridview so I just can't manually edit it in MS Access.

Here is my current code:

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

Answer

In the line where you assign the OleDbDataAdapter you pass in a SELECT * FROM SQL command -

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

Since you know the names of the columns you want and which order you want them in, might it work if you modify the SQL to explicitly name the columns? Example -

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