Sherwin
Sherwin

Reputation: 377

Sorting Specific Column in a GridView after a DataBound

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!~

Upvotes: 1

Views: 10001

Answers (3)

Keith Hodo
Keith Hodo

Reputation: 133

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.

Upvotes: 0

dugas
dugas

Reputation: 12473

You could also use the Gridview.Sort method See Here.

Upvotes: 2

womp
womp

Reputation: 116987

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.

Upvotes: 2

Related Questions