sorting in grid view

asp.net Madrid, Spain
  • 9 years ago

    hi iam creating a grid view in asp.net 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 order.how 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" />

    <Columns>

    <asp:TemplateField>

     

    <itemtemplate>

    <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"

    DynamicServiceMethod="GetUnassignedTeamStepsActions"></ACT:PopupControlExtender>

    </itemtemplate>

     

    </asp:TemplateField>

    <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"/>

     

    </Columns>

    <PagerStyle CssClass="GridPager" />

    <PagerTemplate>

    <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" />

    </PagerTemplate>

    <HeaderStyle CssClass="GridViewHeader" />

    </UCC:GridView>

     

    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("PlanType");

    dtsteps.Columns.Add("ClientName");

    dtsteps.Columns.Add("TaskID");

    dtsteps.Columns.Add("PlanName");

    dtsteps.Columns.Add("StepName");

    dtsteps.Columns.Add("StatusDate");

    dtsteps.Columns.Add("StatusDesc");

    dtsteps.Columns.Add("EstmCompDate");

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

    {

    drsteps = dtsteps.NewRow();

    drsteps[
    "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;

    dtsteps.Rows.Add(drsteps);

    }

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

    {

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

    }

    else

    {

    dvsteps = dtsteps.DefaultView;

    }

    return dvsteps;

    }

     

     

     

    protected void StepsGrid_Sorting(object sender, GridViewSortEventArgs e)

    {

    ViewState[
    "sortExpr"] = e.Sortex-pression;

    stepsGrid.DataSource = stepsBind();

    stepsGrid.DataBind();

    upSteps.Update();

    upStepsRB.Update();

    }

     

     

  • 9 years ago

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

    http://www.dotnetjohn.com/articles.aspx?articleid=77

    Joe 

  • 9 years ago

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

    http://www.netomatix.com/development/GridViewSorting.aspx

    http://aspalliance.com/666_Extending_the_GridViews_Sorting_Capabilities.all

    Hope this will help you.
    Thanks & Regards
    Dilipv 

Post a reply

Enter your message below

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

Contribute

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.

“To iterate is human, to recurse divine” - L. Peter Deutsch