Three sorts of ASP.net girdview learning

Source: Internet
Author: User
Tags sort sorts

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
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.