GridView Sorting and Paging in Asp.net || How to Sort GridView rows in Asp.net || GridView onsorting Event and onpageindexchanging Event in Asp.net:


For GridView paging and sorting write the following code in source file of aspx webpage:
<asp:GridView
              ID="grdCause"
              runat="server
              EnableSortingAndPagingCallback="True"
              AllowPaging="True"
              AllowSorting="True"
              onpageindexchanging="grdCause_PageIndexChanging"
              onsorting="grdCause_Sorting"
         >
</asp:GridView>

The following method is for Loading the Gridview .In this method am taking dataset and bind that dataset to Gridview.
ExecuteProcedure is the method in my sqlhelper class.For that method Click here.
private void LoadGrid()
    {
       
        DataSet ds = dal.ExecuteProcudere("CauseSelectAll", ht);
        grdCause.DataSource = ds.Tables[0];
        grdCause.DataBind();
    }

Gridview pageindexChanging Event  for paging:
protected void grdCause_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdCause.PageIndex = e.NewPageIndex;
        LoadGrid();
    }

Gridview Sorting  Event  for Sorting:

    protected void grdCause_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression;

        if (GridViewSortDirection == SortDirection.Ascending)
        {
            GridViewSortDirection = SortDirection.Descending;
            SortGridView(sortExpression, DESCENDING);
        }
        else
        {
            GridViewSortDirection = SortDirection.Ascending;
            SortGridView(sortExpression, ASCENDING);
        }


    }

Use the following method for Sorting:
private void SortGridView(string sortExpression, string direction)
    {
        //  You can cache the DataTable for improving performance
        LoadGrid();
        DataTable dt = grdCause.DataSource as DataTable;
        DataView dv = new DataView(dt);
        dv.Sort = sortExpression + direction;

        grdCause.DataSource = dv;
        grdCause.DataBind();

    }
    private const string ASCENDING = " ASC";
    private const string DESCENDING = " DESC";

    public SortDirection GridViewSortDirection
    {
        get
        {
            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = SortDirection.Ascending;

            return (SortDirection)ViewState["sortDirection"];
        }
        set { ViewState["sortDirection"] = value;
 }
    }

13 comments:

  1. This was helpful. Thanks!

    ReplyDelete
    Replies
    1. .Net Tutorial: Gridview Sorting And Paging In Asp.Net >>>>> Download Now

      >>>>> Download Full

      .Net Tutorial: Gridview Sorting And Paging In Asp.Net >>>>> Download LINK

      >>>>> Download Now

      .Net Tutorial: Gridview Sorting And Paging In Asp.Net >>>>> Download Full

      >>>>> Download LINK EN

      Delete
  2. This is awesome Thanks..

    ReplyDelete
  3. this is awesome and thanks a lot!

    ReplyDelete
  4. I get the error.
    Compiler Error Message: CS0103: The name 'ht' does not exist in the current context
    Help?

    ReplyDelete
  5. This post is very useful. great work naresh. and what about my blog.
    Developer Queries

    ReplyDelete
  6. Great stuff! There's another tutorial on this at http://www.wiseowl.co.uk/blog/s301/gridview.htm.

    ReplyDelete
  7. Hi ,

    Can u Please help me in I want implemented sorting in window application in c#

    Thanks in advance

    ReplyDelete
  8. what is 'ht'? I removed it and it still needs an argument. help please.

    ReplyDelete
  9. .Net Tutorial: Gridview Sorting And Paging In Asp.Net >>>>> Download Now

    >>>>> Download Full

    .Net Tutorial: Gridview Sorting And Paging In Asp.Net >>>>> Download LINK

    >>>>> Download Now

    .Net Tutorial: Gridview Sorting And Paging In Asp.Net >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete