?
Solved

how to sort asp:gridview when headers are clicked?

Posted on 2010-08-18
2
Medium Priority
?
270 Views
Last Modified: 2013-11-07
i have my gridView code below. everything works fine and now i want to add sorting as a feature to the grid. i have AllowSorting="True" and i added SortExpression="fieldName" to the bound fields i want to make sortable.

im stuck in getting the server side of this figured out.

any help (c# please)?
<asp:GridView ID="gvMilestone" runat="server" AutoGenerateColumns="False" AllowSorting="True"
    AutoGenerateSelectButton="True" CellPadding="4" ForeColor="#333333" GridLines="None"
    Font-Size="8pt" OnRowCommand="GridView_RowCommand" DataKeyNames="ProjectMilestoneId">
    <RowStyle BackColor="#EFF3FB" /> 
    <Columns>
        <asp:BoundField HeaderText="ProjectMilestoneID" DataField="ProjectMilestoneId" />
        <asp:BoundField HeaderText="ProjectId" DataField="ProjectId" />
        <asp:BoundField HeaderText="MilestoneCID" DataField="MileStoneCID" />
        <asp:BoundField HeaderText="Milestone" DataField="MilestoneText" SortExpression="MilestoneText" />
        <asp:BoundField HeaderText="Start Date" DataField="StartDate" SortExpression="StartDate"
            HtmlEncode="false" DataFormatString="{0:dd-MMM-yyyy}" />
        <asp:BoundField HeaderText="End Date" DataField="EndDate" SortExpression="EndDate"
            HtmlEncode="false" DataFormatString="{0:dd-MMM-yyyy}" />
        <asp:BoundField HeaderText="RAGStatusCID" DataField="RAGStatusCID" />
        <asp:BoundField HeaderText="RAG Status" DataField="RAGStatusText" SortExpression="RagStatusText" />
        <asp:BoundField HeaderText="CompletionStatusCID" DataField="CompletionStatusCID" />
        <asp:BoundField HeaderText="Status" DataField="CompletionStatusText" />
        <asp:BoundField HeaderText="StatusText" DataField="StatusText" />
        <asp:BoundField HeaderText="Reporting Priority" DataField="ReportingPriority" SortExpression="ReportingPriority" />
        <asp:BoundField HeaderText="Added" DataField="Added" SortExpression="Added" HtmlEncode="false"
            DataFormatString="{0:dd-MMM-yyyy hh:ss:mm tt}" />
        <asp:BoundField HeaderText="Last Update" DataField="LastUpdate" SortExpression="LastUpdate"
            HtmlEncode="false" DataFormatString="{0:dd-MMM-yyyy hh:ss:mm tt}" />
        <asp:BoundField HeaderText="Update By" DataField="UpdateBy" SortExpression="UpdateBy" />
    </Columns>
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <EditRowStyle BackColor="#2461BF" />
    <AlternatingRowStyle BackColor="White" />
</asp:GridView>

Open in new window

0
Comment
Question by:realcoding
2 Comments
 
LVL 18

Accepted Solution

by:
Anil Golamari earned 2000 total points
ID: 33465070
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)    {              bindGridView(e.SortExpression, sortOrder);    
       }
    public string sortOrder
    {
       get        {
           if (ViewState["sortOrder"].ToString() == "desc")        
    {            
    ViewState["sortOrder"] = "asc";        
    }        
    else      
      {          
     ViewState["sortOrder"] = "desc";      
     }        
    return ViewState["sortOrder"].ToString();    
    }        set    
   {    
       ViewState["sortOrder"] = value;  
     }    }


For reference:

http://programming.top54u.com/post/ASP-Net-2-0-Gridview-Sorting-Using-C-sharp.aspx
0
 

Author Closing Comment

by:realcoding
ID: 33465599
great link.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question