[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

how to sort asp:gridview when headers are clicked?

Posted on 2010-08-18
2
Medium Priority
?
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

656 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