Solved

how to sort asp:gridview when headers are clicked?

Posted on 2010-08-18
2
260 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 500 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now