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

Ascending or Descending?

Ascending or Descending?

With that method, you can only do it one way (we chose Ascending). However, you can make it both. Below, I've created a form with two radio buttons (Ascending and Descending), you chose one of those and click on the column heading.

<form runat="server">
<asp:DataGrid id="dg" runat="server"
Bordercolor="black"
gridlines="vertical"
font-names="Arial"
font-size="10pt"
HeaderStyle-BackColor="#FFCC00"
ItemStyle-BackColor="#FFCC66"
AlternatingItemStyle-BackColor="#FFFFFF"
AutoGenerateColumns="True"
OnEditCommand="dg_edit"
OnCancelCommand="dg_cancel"
OnUpdateCommand="dg_update"
OnSortCommand="dg_sort"
AllowSorting="True"
>
</asp:dataGrid>
<p>
<asp:radiobuttonlist runat="server" id="sortby" repeatdirection="horizontal" textalign="right" >
<asp:listitem selected="true">Ascending</asp:listitem>
<asp:listitem>Descending</asp:listitem>
</asp:radiobuttonlist>
</form>

This is simply the DataGrid with two radio buttons.

sub dg_sort(sender as object, e as DataGridSortCommandEventArgs)
Dim sortby = Request.Form("sortby")
Dim dtable as DataTable = ds.Tables("users")
Dim dview as new DataView(dtable)
If sortby = "Ascending" Then
dview.sort = e.SortExpression.ToString & " ASC"
Else If sortby = "Descending" Then
dview.sort = e.SortExpression.ToString & " DESC"
End If
dg.DataSource = dview
dg.databind()
end sub

All that has changed, is to check which radio button is selected and to use the appropriate sort string.

You can have a whole form with other properties relating to how to sort the data and then use something like this.

Below are some screenshots of the sorting at work.

 


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-26 11:59:35 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search