To set the allowsortring=true of the GridView first so that the sorting events of the GridView are sorted when you click the column headers
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using system.web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Collections.Generic; 13public partial class GridViewSortingTest:System.Web.UI.Page 14{protected void Page_Load (object sender, Eventar GS e) {if (! IsPostBack) {Clientinfoaccessobj accessor = new Clientinfoaccessobj () Gridview1.datasource = acces Sor.
Getallclients ()//bind all customer information gridview1.databind (); 22} 23} 24//sorted by Customer name compare public int comparebyclientname (Clientinfo Client1, Clientinfo Client2) {RE
Turn Client1.ClientName.CompareTo (client2.clientname); 28} 29 30//By postal code and address sorting compare to public int comparebypostcodeandaddressstr (Clientinfo client1, Clientinfo Client2) 32 {3 3 int ret = Client1.PostCode.CompareTo (client2.
postcode); 34 if (Ret!= 0) return ret; else//If the ZIP code is the same as Client1.AddressStr.CompareTo (Client2.
ADDRESSSTR); 39} 40} 41//Sorting by postal code compare the Comparebypostcode int (clientinfo client1, Clientinfo client2) Client1.PostCode.CompareTo (Client2.
postcode); 45} 46//the event being sorted protected void gridview1_sorting (object sender, GridViewSortEventArgs e)-CLIENTINFOACC
Essobj accessor = new Clientinfoaccessobj (); list<clientinfo> clients = accessor.
Getallclients (); I switch (e.sortexpression) "ClientName": Clients.
Sort (comparebyclientname);//parameter is a function name of a comparison<t> type of generic delegate; Case "Multicolumnsort": Clients.
Sort (COMPAREBYPOSTCODEANDADDRESSSTR);
a break; The case "postcode": Clients.
Sort (Comparebypostcode);
The break; Default:63 Clientscript.registerclientscriptblock (this. GetType (), ' infomsg ', ' alert (' does not support sorting this field '; ", true);
The break;
Gridview1.datasource = clients;//binding display data gridview1.databind (); protected void Btnsortbyname_click (object sender, EventArgs e), Gridview1.sort ("ClientName", Sortdire Ction. Ascending)//This event completes and executes sorting event protected void Btnsortbypostcodeandaddress_click (object sender, EventArgs e) 74 {Gridview1.sort ("Multicolumnsort", sortdirection.ascending); 76} 77} 78