Skip to main content

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

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.

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 -

How to validate dropdownlist in JavaScript

In this article you will see how to put validation in dropdownlist by javascript, suppose first item value of dropdownlist is 0 and text is "-Select-" just like given below and we have to validate that at least one item is selected excluding default i.e "-Select-".

Regular expression for alphanumeric with space in asp.net c#

How to validate that string contains only alphanumeric value with some spacial character and with whitespace and how to validate that user can only input alphanumeric with given special character or space in a textbox (like name fields or remarks fields). In remarks fields we don't want that user can enter anything, user can only able to enter alphanumeric with white space and some spacial character like -,. etc if you allow. Some of regular expression given below for validating alphanumeric value only, alphanumeric with whitspace only and alphanumeric with whitespace and some special characters.