I have given an access to stored procedure, which i'm not able to edit. This Stored Procedure returns a Table with 2 Column, what I did is set a GridView's DataSource using SQLDataSource in this stored procedure. but I want this GridView to Sort an specific column to descending whenever this GridView Loads.
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource2" ForeColor="Black"
Width="58%" Height="125px" AllowPaging="True" AllowSorting="True"
PageSize="5" >
<Columns>
<asp:BoundField DataField="DateOccur" HeaderText="Login Date"
开发者_运维技巧 SortExpression="DateOccur" />
<asp:BoundField DataField="TotalMinutesPlayed" HeaderText="Total Minutes"
SortExpression="TotalMinutesPlayed" />
</Columns>
<AlternatingRowStyle BackColor="#EFEFEF" />
</asp:GridView>
Thanks!~
You could also use the Gridview.Sort method See Here.
If you can't sort it by editing the SQL itself, you can sort it programmatically by binding a DataView object to the datasource, specifying a Sort expression for the DataView, and then binding the DataGrid to the DataView object.
For a walkthrough of some example code, see here.
I was using a slightly modified version of the example code I found on MSDN.
Gridview Sort Example on MSDN
By combining the example I found with the SortExpression property I can sort on any column even using proprietary sorts my specification called for.
精彩评论