Csharp GridView Sorting 字段排序用法

复制代码
Default.aspx代码
 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GridViewSorting._Default" %>
 2 
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml" >
 6 <head runat="server">
 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 8     <title>Csharp GridView Sorting 字段排序用法</title>
 9 </head>
10 <body>
11     <form id="form1" runat="server">
12     <div>
13         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnSorting="GridView1_Sorting" AllowPaging="True" AllowSorting="True">
14         <Columns>
15          <asp:TemplateField HeaderText="Id" SortExpression="ApplicationID">
16                     <ItemTemplate>
17                         <asp:Label ID="lblbigid" Text='<%# Eval("ApplicationID") %>' runat="server"></asp:Label>
18                     </ItemTemplate>
19                     <EditItemTemplate>
20                         <asp:Label ID="lblbigid" Text='<%# Eval("ApplicationID") %>' runat="server"></asp:Label>
21                     </EditItemTemplate>
22                     <FooterTemplate>                        
23                     </FooterTemplate></asp:TemplateField>
24                 <asp:TemplateField HeaderText="姓名" SortExpression="ApplicationName">
25                   <ItemTemplate>
26                         <asp:Label ID="lblbigname" Text='<%# Eval("ApplicationName") %>' runat="server"></asp:Label>
27                     </ItemTemplate>
28                     <EditItemTemplate>
29                         <asp:TextBox ID="txtbigname" Text='<%# Eval("ApplicationName") %>' runat="server"></asp:TextBox>
30                     </EditItemTemplate>
31                     <FooterTemplate>
32                         <asp:TextBox ID="txtbigname" Text='' runat="server"></asp:TextBox>
33                     </FooterTemplate>
34                 </asp:TemplateField>
35                                 <asp:TemplateField HeaderText="职称" SortExpression="ServerName">
36                   <ItemTemplate>
37                         <asp:Label ID="lblServerName" Text='<%# Eval("ServerName") %>' runat="server"></asp:Label>
38                     </ItemTemplate>
39                     <EditItemTemplate>
40                         <asp:TextBox ID="txtServerName" Text='<%# Eval("ServerName") %>' runat="server"></asp:TextBox>
41                     </EditItemTemplate>
42                     <FooterTemplate>
43                         <asp:TextBox ID="txtServerName" Text='' runat="server"></asp:TextBox>
44                     </FooterTemplate>
45                 </asp:TemplateField>
46         </Columns>
47         </asp:GridView>
48     
49     </div>
50     </form>
51 </body>
52 </html>
53 
复制代码

 

复制代码
Default.aspx.cs代码
  1 using System;
  2 using System.Data;
  3 using System.Configuration;
  4 using System.Collections;
  5 using System.Web;
  6 using System.Web.Security;
  7 using System.Web.UI;
  8 using System.Web.UI.WebControls;
  9 using System.Web.UI.WebControls.WebParts;
 10 using System.Web.UI.HtmlControls;
 11 
 12 namespace GridViewSorting
 13 {
 14     /// <summary>
 15     /// 2010-09-16 字段排序用法  
 16     /// Geovin Du 涂聚文
 17     /// </summary>
 18     public partial class _Default : System.Web.UI.Page
 19     {
 20         protected void Page_Load(object sender, EventArgs e)
 21         {
 22             if (!IsPostBack)
 23             {
 24                 //首次回载页面排序
 25                 ViewState["sortExpression"= "ApplicationID";
 26                 ViewState["sort"= " DESC";
 27                 GridBind();
 28                 //Binding GridView with the datasource
 29                 //this.GridView1.DataSource = FillDataTable();
 30                 //this.GridView1.DataBind();
 31             }
 32         }
 33         /// <summary>
 34         /// 
 35         /// </summary>
 36         /// <param name="sender"></param>
 37         /// <param name="e"></param>
 38         protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)    
 39         {
 40 
 41             ViewState["sortExpression"= e.SortExpression;
 42             if (ViewState["sort"].ToString() == " DESC")
 43             {
 44                 ViewState["sort"= " ASC";
 45                 GridBind();
 46             }
 47             else
 48             {
 49                 ViewState["sort"= " DESC";
 50                 GridBind();
 51             }
 52 
 53         }
 54         /// <summary>
 55         /// Method that will add few rows in a DataTable and returns a DataTable
 56         /// </summary>
 57         /// <returns>DataTable</returns>
 58         protected DataTable FillDataTable()
 59         {
 60             DataTable dt = new DataTable();
 61             dt.Columns.Add("ApplicationID");
 62             dt.Columns.Add("ApplicationName");
 63             dt.Columns.Add("ServerName");
 64 
 65             DataRow dr = dt.NewRow();
 66             dr[0= "1";
 67             dr[1= "涂斯博";
 68             dr[2= "硬件工程师";
 69             dt.Rows.Add(dr);
 70 
 71             DataRow dr1 = dt.NewRow();
 72             dr1[0= "2";
 73             dr1[1= "杜甫";
 74             dr1[2= "美学工程师";
 75             dt.Rows.Add(dr1);
 76 
 77             DataRow dr2 = dt.NewRow();
 78             dr2[0= "3";
 79             dr2[1= "刘杰";
 80             dr2[2= "网络工程师";
 81             dt.Rows.Add(dr2);
 82 
 83             DataRow dr3 = dt.NewRow();
 84             dr3[0= "4";
 85             dr3[1= "涂聚文";
 86             dr3[2= "软件工程师";
 87             dt.Rows.Add(dr3);
 88 
 89             DataRow dr4 = dt.NewRow();
 90             dr4[0= "5";
 91             dr4[1= "赵金红";
 92             dr4[2= "公共管理师";
 93             dt.Rows.Add(dr4);
 94 
 95             DataRow dr5 = dt.NewRow();
 96             dr5[0= "6";
 97             dr5[1= "江明";
 98             dr5[2= "架构工程师";
 99             dt.Rows.Add(dr5);
100 
101             DataRow dr6 = dt.NewRow();
102             dr5[0= "7";
103             dr5[1= "达成";
104             dr5[2= "数据库工程师";
105             dt.Rows.Add(dr6);
106 
107             return dt;
108         }
109 
110         /// <summary>
111         /// 排序绑定
112         /// </summary>
113         private void GridBind()
114         {
115             //DataSet 用法
116             //DataSet myset = FillDataTable();
117             DataView myview = FillDataTable().DefaultView;// DataSet用法: myset.Tables[0].DefaultView;
118             myview.Sort = ViewState["sortExpression"].ToString() + ViewState["sort"].ToString();
119             GridView1.DataSource = myview;
120             GridView1.DataBind();
121         }
122     }
123 }
124 
复制代码

 

 

posted @   ®Geovin Du Dream Park™  阅读(1709)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2010年9月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 1 2
3 4 5 6 7 8 9
点击右上角即可分享
微信分享提示