4

Can someone share how to practically implement gridview sorting and handle that event if:

  1. The data is binded manually
  2. Gridview is built using template field that is pumped from code behind only (not from the markups)

I build my gridview solely from codebehind therefore I can't use the default method or solution.

Thank you

12
  • Do you want sample code for inbuilt feature of sorting of gridview? Commented Jan 3, 2012 at 6:16
  • have you googled this. weblogs.asp.net/vikram/archive/2008/04/15/… Commented Jan 3, 2012 at 6:17
  • stackoverflow.com/questions/5608049/… Commented Jan 3, 2012 at 6:18
  • for ur second point u can refer this link forums.asp.net/t/1001702.aspx Commented Jan 3, 2012 at 6:20
  • My friend the link you provide indeed satisfy my 1st requirement but it doens't satisfy my 2nd requirement (yes I have tested it). And yes I have googled it. tQ Commented Jan 3, 2012 at 6:21

3 Answers 3

7

This might be what you are looking for:

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    listBindByName(); //this would be your procedure to look for the data you want
    DataSet dsSortTable = GridView1.DataSource as DataSet;
    DataTable dtSortTable = dsSortTable.Tables[0];
    if (dtSortTable != null)
    {
        DataView dvSortedView = new DataView(dtSortTable);
        dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString();
        ViewState["sortExpression"] = e.SortExpression;
        GridView1.DataSource = dvSortedView;
        GridView1.DataBind();
    }
    UpdatePanel1.Update();
}

private string getSortDirectionString()
{
    if (ViewState["sortDirection"] == null)
    {
        ViewState["sortDirection"] = "ASC";
    }
    else
    {
        if (ViewState["sortDirection"].ToString() == "ASC")
        {
            ViewState["sortDirection"] = "DESC";
            return ViewState["sortDirection"].ToString();
        }
        if (ViewState["sortDirection"].ToString() == "DESC")
        {
            ViewState["sortDirection"] = "ASC";
            return ViewState["sortDirection"].ToString();
        }
    }
    return ViewState["sortDirection"].ToString();
}

This is an example of the TemplateField:

<asp:TemplateField HeaderText="Description" SortExpression="description">
    <ItemTemplate>
        <asp:Label Visible="true" runat="server" ID="descriptionLabel" Text='<%# bind("description")  %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtEditDescription" Width="100px" runat="server" Text='<%#Bind("description") %>' />
    </EditItemTemplate>
</asp:TemplateField>

By adding the SortExpression property the GridView header will become clickable. Make sure the sort expression attribute is the name of the field that you are binding through the sql query.

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

How do I achieve something like that with my issue here: stackoverflow.com/questions/25148278/…
1
/* Best to use the shortened routine below - which can be further shortened */        
private string GetSortDirectionString()
{
    if (ViewState["sortDirection"] == null) ViewState["sortDirection"] = "ASC";

    var currDir = ViewState["sortDirection"].ToString().ToUpper();

    switch (currDir)
    {
        case "ASC": ViewState["sortDirection"] = "DESC"; break;
        case "DESC": ViewState["sortDirection"] = "ASC"; break; 
    }

    return ViewState["sortDirection"].ToString();
}

Comments

0

Grid view Sorting In asp .net

Step ONE Add grid view to your page edit the source code by adding allow sorting true and initiating an event by on sorting

<asp:GridView ID="GridView1" AllowSorting="true" OnSorting="GridView1_Sorting"  runat="server">
    </asp:GridView>

Step Two

In the code behind page..we need to handle this event "GridView1_Sorting" and the data table binding. On page load we will bind the datatable with the gridview

 dt = Class1.getDataSet().Tables[0]; // here dt is the datatable object declared Globally.
 GridView1.DataSource = dt; 
 GridView1.DataBind();

so now if we run our code the grid view will be visible but no sorting.

Step Three

Next we need to handle the Gridview Sorting Event. First of all we need to declare one static string SortDirection.

 protected void SetSortDirection(string sortDirection)
        {
            if (sortDirection == "ASC")
            {
                _sortDirection = "DESC";
            }
            else
            {
                _sortDirection = "ASC";
            }
        } 

so the sortDirection is a static string...This function we used to switch between ascendin and descending... Step 4

 protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        SetSortDirection(SortDirection);
        if (dt != null)
        {
            //Sort the data.
            dt.DefaultView.Sort = e.SortExpression + " " + _sortDirection;
            GridView1.DataSource = dt;
            GridView1.DataBind();
            SortDireaction = _sortDirection;
        }
    }

so we have completed our sorting.... sortExpression is nothing but the column name...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.