How to sort only displayed rows in gridview in asp.net using c#

How can we sort the elements of the current page only? And other pages should not be sort in gridview.
In this article you will see how to sort gridview only current page not all records.

We have to do following task for that:-

1) Copying the structure of gridview in a datatable.
2) Copying the data of current page of gridview in that datatabel.
3) Change datatable to dataview.
4) Short the dataview.
5) Then again assigning the sorted data to 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] = "Compleet";
        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] = "UnCompleet";
        dr3[1] = 15;
        dt.Rows.Add(dr3);
        return dt;
    }



Popular Posts