ASP.NET & Databases Part 7
page 3 of 4
by . .
Feedback
Average Rating: 
Views (Total / Last 10 Days): 22821/ 30

The Sort

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.


View Entire Article

User Comments

Title: Nice Article   
Name: Viv
Date: 2004-11-01 2:02:39 AM
Comment:
Good Article.
Keep it up.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-25 4:00:47 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search