GridView Table Header Sort method setting

Source: Internet
Author: User

1.

2. Front Code

Description: Red Code is the core code

<asp:gridview id="Gvdata"runat="Server"autogeneratecolumns="False"onrowdatabound= "Gvdata_rowdatabound"onsorting= "gvdata_sorting" allowsorting= "true"> <Columns> <asp:templatefield headertext="News Headlines"> <ItemTemplate> <asp:label id="Labtitle"runat="Server"text='<%# Bind ("title")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:templatefield headertext="Reading Volume"sortexpression= "Count"> <ItemTemplate> <asp:label id="Labcount"runat="Server"text='<%# Bind ("Count")%>'></asp:Label> </ItemTemplate> <itemstyle width="15%"/> </asp:TemplateField> <asp:templatefield headertext="Add People"> <ItemTemplate> <asp:label id="Labadmin"runat="Server"text='<%# Bind ("AdminName")%>'></asp:Label> </ItemTemplate> <itemstyle width="25%"/> </asp:TemplateField> <asp:templatefield headertext="Time"sortexpression= "ID"> <ItemTemplate>Add:<asp:label id="Labtime"runat="Server"text='<%# Bind ("Times")%>'></asp:Label> <asp:label id="Labtimepass"runat="Server"text='<%# Bind ("Timepass")%>'></asp:Label> <br/> </ItemTemplate> <itemstyle width="30%"/> </asp:TemplateField> </Columns> </asp:gridvi Ew>

3. Backstage Code
protected voidPage_Load (Objectsender, EventArgs e) {        if(!IsPostBack) {viewstate["SortOrder"] ="ID"; The default sort field must be viewstate["Orderdire"] ="Desc"; Default sort, must Filldata ();        }    }    protected voidFilldata () {
    stringSort = (string) viewstate["SortOrder"] +" "+ (string) viewstate["Orderdire"];
    string sql = "select * from Dt_news " +sort;
    
    The DataTable wrote himself, bound to the GridView to write himself, and I would not write here
}
protected voidGvdata_rowdatabound (Objectsender, GridViewRowEventArgs e) {        //determine if a table header        if(E.row.rowtype = =datacontrolrowtype.header) {//determine whether to sort//is the sort field information             for(inti =0; i < E.row.cells.count; i++) {ControlCollection cons=E.row.cells[i].                Controls; if(Cons. Count = =1) {LinkButton lb= cons[0] asLinkButton; if(LB! =NULL)                    {                        if(viewstate["SortOrder"]. ToString () = =lb.commandargument) {if(viewstate["Orderdire"]. ToString () = ="Desc") {lb. Text+="▼"; }                            Else{lb. Text+="▲"; }                        }                    }                }            }        }    }
protected voidGvdata_sorting (Objectsender, GridViewSortEventArgs e) {        stringSpage =e.sortexpression; if(viewstate["SortOrder"]. ToString () = =spage) {            if(viewstate["Orderdire"]. ToString () = ="Desc") viewstate["Orderdire"] ="ASC"; Elseviewstate["Orderdire"] ="Desc"; }        Else{viewstate["SortOrder"] =e.sortexpression; } Pagecontrol1.pagenow=1;    Filldata (); }

GridView Table Header Sort method setting

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.