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

Uploading large file in chunks in Asp.net Mvc c# from Javascript ajax

Often we have a requirement to upload files in Asp.net, Mvc c# application but when it comes to uploading larger file, we always think how to do it as uploading large file in one go have many challenges like UI responsiveness, If network fluctuate for a moment in between then uploading task get breaks and user have to upload it again etc.

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.

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)

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.