In ASP. net2.0, The gridview Control completes code to sort template columns! (Code debugging passed)

Source: Internet
Author: User
1 Protected   Void Page_load ( Object Sender, eventargs E)
2 {
3 If ( ! Ispostback)
4 {
5 // Bind data on the initialization page to the gridview
6 Getdataset ();
7 Gridviewbind ();
8 }
9 }
10
11 ///   <Summary>
12 /// Generate DS Dataset
13 ///   </Summary>
14 Private Dataset getdataset ()
15 {
16 // String strsql = "select * From Manager. db1 ";
17 String Strsql =   " Select * From Manager. db1 " ;
18 Dataset DS = Conn. getds (strsql );
19 Return DS;
20 }
21
22 ///   <Summary>
23 /// Bind data to the gridview
24 ///   </Summary>
25 Private   Void Gridviewbind ()
26 {
27 This . Gridview1.datasource =   This . Getdataset (). Tables [ 0 ]. Defaultview;
28 This . Gridview1.databind ();
29 }
30 ///   <Summary>
31 /// Stores the current sorting status of selected columns
32 ///   </Summary>
33 Public Sortdirection gridviewsortction ction
34 {
35 Get
36 {
37 If (Viewstate [ " Sortdirection " ] =   Null )
38 Viewstate [ " Sortdirection " ] = Sortdirection. ascending;
39 Return (Sortdirection) viewstate [ " Sortdirection " ];
40 }
41 Set {Viewstate [ " Sortdirection " ] = Value ;}
42 }
43
44 Protected   Void Gridviewappssorting ( Object Sender, gridviewsorteventargs E)
45 {
46 String Sortexpression = E. sortexpression;
47 If (Gridviewsortction ction = Sortdirection. ascending)
48 {
49 Gridviewsortction ction = Sortdirection. Descending;
50 Sortgridview (sortexpression, " Desc " );
51 }
52 Else
53 {
54 Gridviewsortction ction = Sortdirection. ascending;
55 Sortgridview (sortexpression, " ASC " );
56 }
57 }
58
59 ///   <Summary>
60 /// Sort and bind
61 ///   </Summary>
62 Private   Void Sortgridview ( String Sortexpression, String Direction)
63 {
64 Datatable dt =   This . Getdataset (). Tables [ 0 ];
65 Dataview dv =   New Dataview (DT );
66
67 DV. Sort = Sortexpression +   "   "   + Direction;
68
69 Gridview1.datasource = DV;
70 Gridview1.databind ();
71 }

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.