Sorting only current page of Gridview in asp.net c#

In this article I will explain how to sort Gridview only current page not all records (sorting the displayed rows of current page only).

We have to do following task for that:-
  1. Copy the schema of Gridview into a Datatable.
  2. Copy the data of current page of Gridview into Datatable.
  3. Create Dataview of the Datatable.
  4. Sort the Dataview.
  5. Then again assigning the sorted data to Gridview.

Code to sort only current page of Asp.net Gridview



public void Sort_CurrentPage(string colname, GridView grd)
    {
        //***Coping Structure of gridview to a datatable******
        DataTable dtclone = new DataTable();
        for (int j = 0; j < grd.HeaderRow.Cells.Count; j++)
        {
            dtclone.Columns.Add(grd.HeaderRow.Cells[j].Text);
        }

        //*****Coping gridview data to a datatable*******
        for (int i = 0; i < grd.Rows.Count; i++)
        {
            DataRow dr = dtclone.NewRow();
            for (int j = 0; j < grd.HeaderRow.Cells.Count; j++)
            {
                dr[j] = grd.Rows[i].Cells[j].Text;
              
              //If Gridview have all template column
              //  dr[j] = (grd.Rows[i].Cells[j].Controls[1] as Label).Text;

               //If Gridview have all Bound column
              //  dr[j] = (grd.Rows[i].Cells[j].Controls[0] as Label).Text;

               
            }
            dtclone.Rows.Add(dr);
        }

        //********Sorting data of datatable*************
        DataView dv = new DataView(dtclone);
        if (ViewState["SortingOrder"] == null)
        {
            dv.Sort = colname;
            ViewState["SortingOrder"] = 1;
        }
        else
        {
            dv.Sort = colname + " desc";
            ViewState["SortingOrder"] = null;
        }

       //******Assigning back to gridview sorted data******
        for (int i = 0; i < grd.Rows.Count; i++)
        {
            for (int j = 0; j < grd.HeaderRow.Cells.Count; j++)
            {
                grd.Rows[i].Cells[j].Text = dv[i][j].ToString();


              //If Gridview have all template column
              // (grd.Rows[i].Cells[j].Controls[1] as Label).Text = dv[i][j].ToString();

              //If Gridview have all Bound column
             // (grd.Rows[i].Cells[j].Controls[0] as Label).Text = dv[i][j].ToString();

            }

        }

    }

Then on your header link
Sort_CurrentPage("ColumnName", GridView1);


protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.DataSource = GetGraphData();
            GridView1.DataBind();
        }
    }
    private System.Data.DataTable GetGraphData()
    {

        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[2] { new DataColumn("ProjectStatus"), new DataColumn("per") });

        DataRow dr1 = dt.NewRow();
        dr1[0] = "Complete";
        dr1[1] = 10;
        dt.Rows.Add(dr1);

        DataRow dr2 = dt.NewRow();
        dr2[0] = "Pending";
        dr2[1] = 20;
        dt.Rows.Add(dr2);

        DataRow dr3 = dt.NewRow();
        dr3[0] = "UnComplete";
        dr3[1] = 15;
        dt.Rows.Add(dr3);
        return dt;
    }



Popular Posts