Skip to main content

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 from this blog

how $document.ready() is different from window.onload()

we often use window.onload() in javascript and $document.ready() in jquery and assume that both are same just jquery library wrap up javascript window.onload() and introduce $document.ready() but no there is much difference between them below is the explanation -

regex - check if a string contains only alphabets c#

How to validate that input string contains only alphabets, validating that textbox contains only alphabets (letter), so here is some of the ways for doing such task. char have a property named isLetter which is for checking if character is a letter or not, or you can check by the regular expression  or you can validate your textbox through regular expression validator in asp.net. Following code demonstrating the various ways of implementation.

Merging multiple PDFs using iTextSharp in asp.net c#

In this article i will show you how to merge multiple pdfs into one using ITextSharp below is the two approach one is to pass your input files path, output file path (will be created if not exist) and another is pass direct input stream, output stream and it will write the merge files into output stream.

How to handle click event of linkbutton inside gridview

Recently I have posted how to sort only current page of gridview , Scrollble gridview with fixed header through javascript , File upload control inside gridview during postback and now i am going to explain how to handle click event of linkbutton or any button type control inside gridview. We can handle click event of any button type control inside gridview by two way first is through event bubbling and second one is directly (in this type of event handling we need to access current girdviewrow container)