PDA

View Full Version : sort the grid


handshakeit
05-23-2006, 07:45 AM
Hello

I want to know how to sort the records in a Gridview control according to each collumn in both ways ie.ascending and descending order....
plz help it is very urgent.

Malith
05-23-2006, 09:32 AM
<asp:GridView ID="GridView1" AllowSorting="true" runat="server" DataSourceID="SqlDataSource1"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="ID" DataField="au_id" SortExpression="au_id" />
<asp:BoundField HeaderText="Last Name" DataField="au_lname" SortExpression="au_lname" />
<asp:BoundField HeaderText="First Name" DataField="au_fname" SortExpression="au_fname" />
<asp:BoundField HeaderText="Phone" DataField="phone" SortExpression="phone" />
<asp:BoundField HeaderText="Address" DataField="address" SortExpression="address" />
<asp:BoundField HeaderText="City" DataField="city" SortExpression="city" />
<asp:BoundField HeaderText="State" DataField="state" SortExpression="state" />
<asp:BoundField HeaderText="Zip Code" DataField="zip" SortExpression="zip" />
<asp:CheckBoxField HeaderText="Contract" SortExpression="contract" DataField="contract" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
SelectCommand="SELECT [au_id], [au_lname], [au_fname], [phone], [address], [city], [state], [zip], [contract] FROM [authors]"
ConnectionString="<%$ ConnectionStrings:Pubs %>" />


cheers

Malith

handshakeit
05-23-2006, 01:15 PM
its ok but i hav to show two different buttons in the header one for sort in ascending order and other for descending order
any idea how to do it ?????

NuneatonSam
05-24-2006, 02:09 AM
You could do it with the use of a querystring, for example in your querystring have

order=asc or desc

then in your sql have something like Select from etc etc order by name & request.querystring

You then obviously have buttons which point to different querystrings.