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