sorting in grid view Madrid, Spain
  • 12 years ago

    hi iam creating a grid view in 2.0

    and iam binding fields in to it:

    my problem is:

    iam able to sort in ascending order,but wat i need is if i click the header first time it should sort in ascending order and if i click again it should sort in descending can i do that

    this is my coding:

    <UCC:GridView ID="stepsGrid" runat="server" Width="100%" GridLines="None" EnableViewState="false"

    HeaderStyle-CssClass="GridViewHeader" DataKeyNames="TaskID" AllowPaging="true"

    PageSize="10" CssClass="GridView" OnPageIndexChanging="StepsGrid_PageIndexChanging"

    AllowSorting="true" OnSorting="StepsGrid_Sorting" AutoGenerateColumns="false" >

    <AlternatingRowStyle BackColor="#FBF5F6" />





    <asp:Panel id="popupStep" runat="server" CssClass="pnlpopup" Width="98px"></asp:Panel>

    <asp:ImageButton id="stepGridButton" runat="server" HorizontalAlign="Center" ImageUrl="~/Images/CRM/menu_exp_.gif"></asp:ImageButton>

    <ACT:PopupControlExtender id="PopupControlExtender3" runat="server" Position="right"

    PopupControlID="popupStep" TargetControlID="stepGridButton" DynamicControlID="popupStep"

    DynamicContextKey='<%# ReassignStepPopupControl(Eval("TaskID"),Eval("StepName")) %>' DynamicServicePath="WebMethods.aspx"





    <asp:BoundField HeaderText="Task Type" Sortex-pression="PlanType" DataField="PlanType"/>

    <asp:BoundField HeaderText="Client Name" Sortex-pression="ClientName" DataField="ClientName" />

    <asp:BoundField HeaderText="Task ID" Sortex-pression="TaskID" DataField="TaskID"/>

    <asp:BoundField HeaderText="Task Name" Sortex-pression="PlanName" DataField="PlanName"/>

    <asp:BoundField HeaderText="Action Step" Sortex-pression="StepName" DataField="StepName"/>

    <asp:BoundField HeaderText="Status Date" Sortex-pression="StatusDate" DataField="StatusDate"/>

    <asp:BoundField HeaderText="Status Description" Sortex-pression="StatusDesc" DataField="StatusDesc"/>

    <asp:BoundField HeaderText="End Date" Sortex-pression="EstmCompDate" DataField="EstmCompDate"/>

    <asp:BoundField HeaderText="Task Owner" Sortex-pression="TaskAssignedTo" DataField="TaskAssignedTo"/>



    <PagerStyle CssClass="GridPager" />


    <asp:Button ID="GoFirstPage" runat="server" CommandName="Page" ToolTip="First Page"

    CommandArgument="First" CssClass="first" />

    <asp:Button ID="PrevPage" runat="server" CommandName="Page" ToolTip="Prev." CommandArgument="Prev"

    CssClass="prev" />

    <asp:Button ID="NextPage" runat="server" CommandName="Page" ToolTip="Next" CommandArgument="Next"

    CssClass="next" />

    <asp:Button ID="GoLastPage" runat="server" CommandName="Page" ToolTip="Last Page"

    CommandArgument="Last" CssClass="last" />


    <HeaderStyle CssClass="GridViewHeader" />



    and my code behind function is:


    private DataView stepsBind()


    DataTable dtsteps = new DataTable();

    DataRow drsteps;

    DataView dvsteps;

    SvcTask svcTasksStep = new SvcTask();

    Steps steps = new Steps();

    steps = svcTasksStep.GetUnassignedSteps(Unisys.TM.UICommon.Helper.SessionManager.UserData.UserTeam);









    dtsteps.Columns.Add("TaskAssignedTo");for (int i = 0; i < steps.Count; i++)


    drsteps = dtsteps.NewRow();

    "PlanType"] = steps[i].PlanType;

    drsteps["ClientName"] = steps[i].ClientName;

    drsteps["TaskID"] = steps[i].TaskID;

    drsteps["PlanName"] = steps[i].PlanName;

    drsteps["StepName"] = steps[i].StepName;

    drsteps["StatusDate"] = steps[i].StatusDate;

    drsteps["StatusDesc"] = steps[i].StatusDesc;

    drsteps["EstmCompDate"] = steps[i].EstmCompDate;

    drsteps["TaskAssignedTo"] = steps[i].TaskAssignedTo;



    if (ViewState["sortExpr"] != null)


    dvsteps =
    new DataView(dtsteps);dvsteps.Sort = (string)ViewState["sortExpr"];




    dvsteps = dtsteps.DefaultView;


    return dvsteps;





    protected void StepsGrid_Sorting(object sender, GridViewSortEventArgs e)


    "sortExpr"] = e.Sortex-pression;

    stepsGrid.DataSource = stepsBind();







  • 12 years ago

    I think you'll find what you're looking for here:


  • 12 years ago

    hi rajkumartwok ,
    you want sorting to be done in both order (Ascending and Descending). Look at the given article's.

    Hope this will help you.
    Thanks & Regards

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook