dg_sort
The method to sort (dg_sort) is like any other
of the DataGrid commands that you have been working in, however it takes a
different parameter in and needs to sort a DataView.
What's a DataView?
A DataView is a view of your DataSet, when
you assigned your DataTable to your DataGrid, it assigned the default
DataView to the DataGrid. The default view is just a direct snapshot of what
the data looked like when it came out of the database.
You can create new DataViews and change their properties to manipulate the
DataTable.
We won't be going into the full workings of a
DataView, just one property, the sort property.
sub
dg_sort(sender as object, e as DataGridSortCommandEventArgs)
Dim dtable as DataTable = ds.Tables("users")
Dim dview as new DataView(dtable)
dview.sort = e.SortExpression.ToString & " ASC"
dg.DataSource = dview
dg.databind()
end sub |
This method simply creates a DataTable which is
the table in your DataSet (the users table) and calls it dtable. The next line
creates a new DataView of that table (dview).
The next line sets the sort property of the DataView which consists of - "The
Column to sort by" & Ascending | Descending. You've probably seen that in the
parameters we're using DataGridSortCommandEventArgs as the EventArgs and this
contains the SortExpression (the column name). We simply assign that to the
sort property and specify that we want ASC or Ascending data.
Then we set the DataGrid's DataSource to the new DataView and DataBind()
That wasn't too hard, was it?
Now try clicking on the different column
headings to sort the data.