Question How to put data from tables in MS Access into an Array in VB

Manny123

Member
Joined
Mar 6, 2012
Messages
16
Programming Experience
Beginner
Dim cn As String = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Database genk.mdb;")
    Dim SQLString As String = "SELECT * FROM Products"
    Dim DBConn1 As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection(cn)
    Dim DataSet1 As New DataSet()
    Dim DataAdapter1 As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter(SQLString, DBConn1)
    DataAdapter1.Open()
    DataAdapter1.Fill(DataSet1, "Products")
    DataGrid_Sort.DataSource = DataSet1.Tables("Products")
    End Sub


When the user clicks the button the all the data in my Products table is displayed on a datagrid. How can i put the data inside my Products table into an array so i can manipulate my fields to undergo quick sort.

P.S. could i also get tips on how to quick sort my data when the user clicks the sort button
 
There's no specific reason to use an array just for sorting. The DataTable you're already using supports sorting. To be more precise, the DataView it's associated with does. In fact, your user can simply click a column header in the grid to sort by that column.

If you want to sort in code then you should use a BindingSource. Add one to your form in the designer, bind your DataTable to it and then bind that to the grid. You then simply set the Sort property of the BindingSource using a SQL ORDER BY clause, e.g. "SomeColumn DESC".

By the way, I assume that you're actually using a DataGridView rather than a DataGrid. If you're not then you should be. Also, your DataSet is pointless if you're just using one DataTable. If all you need is a DataTable then just create a DataTable.
 

Latest posts

Back
Top