asp.net, click on the GridView table header to achieve the sorting of data

Source: Internet
Author: User
first, the basic work to achieve this function.

1, first add a GridView, named Gvdata.

2, set the properties of the control: the steps are as follows.


Set properties:


These 4 properties, and you want to set the control allowsorting= "True", 5 points are indispensable.

3, write the control of the Gvdata_sorting event


Double-click, go in to write. second, code implementation

Usingsystem.data;
 
UsingSystem.Data.SqlClient; Namespace sort {Public partial class WebForm2:System.Web.UI.Page {protected Voidpage_load (object sender, Ev Entargs e) {if (!               
                IsPostBack) {//Give it a default state first, because the following bin () method requires a state value.
 
                viewstate["SortOrder"] = "depid";
 
                viewstate["Orderdire"] = "ASC";
                Bind ();
        Click the DataGrid header field to trigger the event, pass the DataField value of the corresponding field, and reset the viewstate["SortOrder" and viewstate["Orderdire" properties. Protected voidgvdata_sorting (Objectsender, GridViewSortEventArgs e) {stringspage = E.sortexpressi
 
            On if (viewstate["SortOrder"]. ToString () = = Spage) {if (viewstate["Orderdire"].
                ToString () = = "Desc") viewstate["orderdire"] = "ASC";
            else viewstate["orderdire"] = "Desc";
  } else {              viewstate["SortOrder"] =e.sortexpression;
        Bind ();
        //Get the data, sort by customizing the view, and reorder. Public Voidbind () {SqlConnection con= New SqlConnection ("server=.;
            database=department;uid= ' sa ';p wd= ' 123456 '); Con.
            Open ();
            String str= "select * from Tdepartment";
            SqlCommand cmd= New SqlCommand (Str,con);
            Sqldataadaptersqlda = new SqlDataAdapter (cmd);
            Datatabletabdata = new DataTable ();
            Sqlda.fill (Tabdata);
            Sort by customizing the view Dataviewview = Tabdata.defaultview;
            Stringsort = (string) viewstate["SortOrder"] + "" + (String) viewstate["Orderdire"]; View.
 
            sort = sort;         
            Gvdata.datasource = view;
            Gvdata.databind (); Con.         
        Close (); }
   }
}
Three, the realization clicks the table header, realizes the rearrangement, the effect is as follows:

Iv. Summary

It's hard to feel when you're not getting to know what needs are, and it's not that hard when it comes to getting to know the needs, it's really not that hard when it really comes true. Difficult things, always want to escape the difficult heart. Many facts prove: As long as you believe in yourself, calm face difficulties, many problems will no longer be a problem. So...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.