protected void lbGrossProfitRate_Click(object sender, EventArgs e)
{
this.SetSortTip(sender);
}
private void SetSortTip(object sender)
{
//过于复杂,应采用head的方式
LinkButton sendLinkButton = (LinkButton)sender;
SortDirection sd = SortDirection.Ascending;
if (sendLinkButton.Text.IndexOf("▼") > 0)
{
sd = SortDirection.Descending;
}
sd = sd == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;
this.lbReserveDate.Text = this.lbReserveDate.Text.Replace("▼", string.Empty).Replace("▲", string.Empty);
this.lbReserveQuantity.Text = this.lbReserveQuantity.Text.Replace("▼", string.Empty).Replace("▲", string.Empty);
this.lbReserveTicketQuantity.Text = this.lbReserveTicketQuantity.Text.Replace("▼", string.Empty).Replace("▲", string.Empty);
this.lbInParkQuantity.Text = this.lbInParkQuantity.Text.Replace("▼", string.Empty).Replace("▲", string.Empty);
this.lbInParkTicketQuantity.Text = this.lbInParkTicketQuantity.Text.Replace("▼", string.Empty).Replace("▲", string.Empty);
this.lbInParkRate.Text = this.lbInParkRate.Text.Replace("▼", string.Empty).Replace("▲", string.Empty);
this.lbSaleAmount.Text = this.lbSaleAmount.Text.Replace("▼", string.Empty).Replace("▲", string.Empty);
this.lbCommissionAmount.Text = this.lbCommissionAmount.Text.Replace("▼", string.Empty).Replace("▲", string.Empty);
this.lbGrossProfitRate.Text = this.lbGrossProfitRate.Text.Replace("▼", string.Empty).Replace("▲", string.Empty);
sendLinkButton.Text += sd == SortDirection.Ascending ? " ▲" : " ▼";
this.VsSortExpression = string.Format("{0} {1}", sendLinkButton.CommandName, sd == SortDirection.Ascending ? "ASC" : "DESC");
this.BindData();
}
<th><asp:LinkButton ID="lbReserveDate" runat="server" OnClick="lbReserveDate_Click" CommandName="ReserveDate">预订日期</asp:LinkButton></th>
<th><asp:LinkButton ID="lbReserveQuantity" runat="server" OnClick="lbReserveQuantity_Click" CommandName="ReserveQuantity">预订单数</asp:LinkButton></th>
<th><asp:LinkButton ID="lbReserveTicketQuantity" runat="server" OnClick="lbReserveTicketQuantity_Click" CommandName="ReserveTicketQuantity">预订票数</asp:LinkButton></th>
<th><asp:LinkButton ID="lbInParkQuantity" runat="server" OnClick="lbInParkQuantity_Click" CommandName="InParkQuantity">入园单数</asp:LinkButton></th>
<th><asp:LinkButton ID="lbInParkTicketQuantity" runat="server" OnClick="lbInParkTicketQuantity_Click" CommandName="InParkTicketQuantity">入园票数</asp:LinkButton></th>
<th><asp:LinkButton ID="lbInParkRate" runat="server" OnClick="lbInParkRate_Click" CommandName="InParkRate">入园率</asp:LinkButton></th>
<th><asp:LinkButton ID="lbSaleAmount" runat="server" OnClick="lbSaleAmount_Click" CommandName="SaleAmount">销售金额</asp:LinkButton></th>
<th><asp:LinkButton ID="lbCommissionAmount" runat="server" OnClick="lbCommissionAmount_Click" CommandName="CommissionAmount">佣金金额</asp:LinkButton></th>
<th><asp:LinkButton ID="lbGrossProfitRate" runat="server" OnClick="lbGrossProfitRate_Click" CommandName="GrossProfitRate">毛利率</asp:LinkButton></th>
DataColumn inParkRate = new DataColumn("InParkRate", typeof(decimal));
DataColumn grossProfitRate = new DataColumn("GrossProfitRate", typeof(decimal));
dt.Columns.Add(inParkRate);
dt.Columns.Add(grossProfitRate);
foreach (DataRow dr in dt.Rows)
{
dr["InParkRate"] = this.GetInParkRate(dr["InParkTicketQuantity"], dr["ReserveTicketQuantity"]);
dr["GrossProfitRate"] = this.GetGrossProfitRate(dr["CommissionAmount"], dr["SaleAmount"]);
}
DataView dv = dt.DefaultView;
dv.Sort = this.VsSortExpression;
//方便分面
public string VsSortExpression
{
get
{
if (ViewState["sortExpression"] == null)
ViewState["sortExpression"] = string.Empty;
return (string)ViewState["sortExpression"];
}
set { ViewState["sortExpression"] = value; }
}
建议使用DataView
public SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;
return (SortDirection)ViewState["sortDirection"];
}
set { ViewState["sortDirection"] = value; }
}
protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;
if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, " DESC");
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, " ASC");
}
}
private void SortGridView(string sortExpression, string direction)
{
DataTable dt = GetBind();
//DataView dv = new DataView(dt);
dt.DefaultView.Sort = sortExpression + direction;
GridView1.DataSource = Pagination(dt, Pager1, false);
GridView1.DataBind();
}
===================================
以下转自http://www.cnblogs.com/vaiyanzi/archive/2007/03/23/685871.html
做项目的时候,DataGrid ,DataList,Repeater 三个控件都是很优秀的数据显示控件,DataGrid的方便,简单易用,功能强大,但对性能会有所影响,在loading页面的时候大量的Html会占用一些时间,DataList 较之DataGrid功能显然减少一些,经常在一些特殊的页面制作或需求中来用,经常简单的页面显示收据表格,使用Repeater+Table即方便又快捷,但是却没有DataGrid那样方便,最近做项目,全部使用Repeater,但是客户突然要求要对数据表格进行排序,惨了~~~~~,第一印象就是DataGrid控件有排序功能,要换控件吗?靠,那么多表单,全部换DataGrid会出人命,于是就想办法,来实现Repeater的排序。下面就是我做的一个Repeater实现双向排序的例子:主要使用LinkButton,Repeater,ViewState,Cache结合来完成的。
效果如下图:
下载原代码:/Files/vaiyanzi/Attch/RepeaterOrder.rar
前台Html代码:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML >
< HEAD >
< title > RepeaterOrderForm </ title >
< meta name ="GENERATOR" Content ="Microsoft Visual Studio .NET 7.1" >
< meta name ="CODE_LANGUAGE" Content ="C#" >
< meta name ="vs_defaultClientScript" content ="JavaScript" >
< meta name ="vs_targetSchema" content ="http://schemas.microsoft.com/intellisense/ie5" >
</ HEAD >
< body MS_POSITIONING ="GridLayout" >
< form id ="Form1" method ="post" runat ="server" >
< table border =1 bordercolor =#330033 bordercolordark =#999933 bordercolorlight =black >
< tr >
< td colspan =5 align =center >< font color =blue size =12 > Repeater实现双向排序功能 </ font ></ td >
</ tr >
< asp:Repeater id ="rpOrder" runat ="server" >
< HeaderTemplate >
< tr align =center >
< td >
< asp:LinkButton ID ="LastName" Runat ="server" text ="LastName" CommandName ="LastName" ></ asp:LinkButton ></ td >
< td >
< asp:LinkButton ID ="FirstName" Runat ="server" text ="FirstName" CommandName ="FirstName" ></ asp:LinkButton ></ td >
< td >
< asp:LinkButton ID ="Title" Runat ="server" text ="Title" CommandName ="Title" ></ asp:LinkButton ></ td >
< td >
< asp:LinkButton ID ="Address" Runat ="server" text ="Address" CommandName ="Address" ></ asp:LinkButton ></ td >
< td >
< asp:LinkButton ID ="City" Runat ="server" text ="City" CommandName ="City" ></ asp:LinkButton ></ td >
</ tr >
</ HeaderTemplate >
< ItemTemplate >
< tr >
< td > <% # DataBinder.Eval(Container.DataItem,"LastName") %> </ td >
< td > <% # DataBinder.Eval(Container.DataItem,"FirstName") %> </ td >
< td > <% # DataBinder.Eval(Container.DataItem,"Title") %> </ td >
< td > <% # DataBinder.Eval(Container.DataItem,"Address") %> </ td >
< td > <% # DataBinder.Eval(Container.DataItem,"City") %> </ td >
</ tr >
</ ItemTemplate >
</ asp:Repeater >
</ table >
</ form >
</ body >
</ HTML >
后台CS文件:
1using System;
2using System.Collections;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Web;
7using System.Web.SessionState;
8using System.Web.UI;
9using System.Web.UI.WebControls;
10using System.Web.UI.HtmlControls;
11using System.Data.SqlClient;
12using System.Configuration;
13
14namespace RepeaterOrder
15{
16 /// <summary>
17 /// RepeaterOrderForm 的摘要说明。
18 /// </summary>
19 public class RepeaterOrderForm : System.Web.UI.Page
20 {
21 protected System.Web.UI.WebControls.Repeater rpOrder;
22 private readonly string CONNECTIONSTRING=ConfigurationSettings.AppSettings["ConnectionString"];
23 private void Page_Load(object sender, System.EventArgs e)
24 {
25 if(!IsPostBack)
26 {
27 BindRepeater();
28 }
29 }
30 private DataView GetData
31 {
32 get
33 {
34 return Cache["_data"] as DataView;
35 }
36 set
37 {
38 if( Cache["_data"]==null)
39 Cache["_data"]=value;
40 }
41 }
42 Web 窗体设计器生成的代码
64
65 private SqlConnection Conn()
66 {
67 return new SqlConnection(CONNECTIONSTRING);
68 }
69
70 private void BindRepeater()
71 {
72 DataSet ds=new DataSet();
73 SqlDataAdapter da=new SqlDataAdapter("Select * From Employees",Conn());
74 da.Fill(ds);
75 GetData=ds.Tables[0].DefaultView;
76 rpOrder.DataSource=ds.Tables[0];
77 rpOrder.DataBind();
78 }
79
80 private void rpOrder_ItemCommand(object source, RepeaterCommandEventArgs e)
81 {
82 if(e.Item.ItemType==ListItemType.Header)
83 {
84 LinkButton lkbtnSort=(LinkButton)e.Item.FindControl(e.CommandName.Trim());
85 if(ViewState[e.CommandName.Trim()]==null)
86 {
87 ViewState[e.CommandName.Trim()]="ASC";
88 lkbtnSort.Text=lkbtnSort.Text+"▲";
89 }
90 else
91 {
92 if(ViewState[e.CommandName.Trim()].ToString().Trim()=="ASC")
93 {
94 ViewState[e.CommandName.Trim()]="DESC";
95 if(lkbtnSort.Text.IndexOf("▲")!=-1)
96 lkbtnSort.Text=lkbtnSort.Text.Replace("▲","▼");
97 else
98 lkbtnSort.Text=lkbtnSort.Text+"▼";
99 }
100 else
101 {
102 ViewState[e.CommandName.Trim()]="ASC";
103 if(lkbtnSort.Text.IndexOf("▼")!=-1)
104 lkbtnSort.Text=lkbtnSort.Text.Trim().Replace("▼","▲");
105 else
106 lkbtnSort.Text=lkbtnSort.Text+"▲";
107 }
108 }
109 ViewState["text"]=lkbtnSort.Text;
110 ViewState["id"]=e.CommandName.Trim();
111 DataView dv=GetData;
112 dv.Sort=e.CommandName.ToString().Trim()+" "+ViewState[e.CommandName.Trim()].ToString().Trim();
113 rpOrder.DataSource=dv;
114 rpOrder.DataBind();
115 }
116 }
117
118 private void rpOrder_ItemDataBound(object sender, RepeaterItemEventArgs e)
119 {
120 if(e.Item.ItemType==ListItemType.Header)
121 {
122 if(ViewState["id"]!=null)
123 {
124 LinkButton lkbtnSort=(LinkButton)e.Item.FindControl(ViewState["id"].ToString().Trim());
125 lkbtnSort.Text=ViewState["text"].ToString();
126 }
127 }
128 }
129 }
130}
131
=============================
DataTable添加列和行的方法
方法一:
DataTable tblDatas = new DataTable("Datas");
DataColumn dc = null;
dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
dc.AutoIncrement = true;//自动增加
dc.AutoIncrementSeed = 1;//起始为1
dc.AutoIncrementStep = 1;//步长为1
dc.AllowDBNull = false;//
dc = tblDatas.Columns.Add("Product", Type.GetType("System.String"));
dc = tblDatas.Columns.Add("Version", Type.GetType("System.String"));
dc = tblDatas.Columns.Add("Description", Type.GetType("System.String"));
DataRow newRow;
newRow = tblDatas.NewRow();
newRow["Product"] = "大话西游";
newRow["Version"] = "2.0";
newRow["Description"] = "我很喜欢";
tblDatas.Rows.Add(newRow);
newRow = tblDatas.NewRow();
newRow["Product"] = "梦幻西游";
newRow["Version"] = "3.0";
newRow["Description"] = "比大话更幼稚";
tblDatas.Rows.Add(newRow);
方法二:
DataTable tblDatas = new DataTable("Datas");
tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
tblDatas.Columns[0].AutoIncrement = true;
tblDatas.Columns[0].AutoIncrementSeed = 1;
tblDatas.Columns[0].AutoIncrementStep = 1;
tblDatas.Columns.Add("Product", Type.GetType("System.String"));
tblDatas.Columns.Add("Version", Type.GetType("System.String"));
tblDatas.Columns.Add("Description", Type.GetType("System.String"));
tblDatas.Rows.Add(new object[]{null,"a","b","c"});
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
方法三:
DataTable table = new DataTable ();
//创建table的第一列
DataColumn priceColumn = new DataColumn();
//该列的数据类型
priceColumn.DataType = System.Type.GetType("System.Decimal");
//该列得名称
priceColumn.ColumnName = "price";
//该列得默认值
priceColumn.DefaultValue = 50;
// 创建table的第二列
DataColumn taxColumn = new DataColumn();
taxColumn.DataType = System.Type.GetType("System.Decimal");
//列名
taxColumn.ColumnName = "tax";
//设置该列得表达式,用于计算列中的值或创建聚合列
taxColumn.Expression = "price * 0.0862";
// Create third column.
DataColumn totalColumn = new DataColumn();
totalColumn.DataType = System.Type.GetType("System.Decimal");
totalColumn.ColumnName = "total";
//该列的表达式,值是得到的是第一列和第二列值得和
totalColumn.Expression = "price + tax";
// 将所有的列添加到table上
table.Columns.Add(priceColumn);
table.Columns.Add(taxColumn);
table.Columns.Add(totalColumn);
//创建一行
DataRow row = table.NewRow();
//将此行添加到table中
table.Rows.Add(row);
//将table放在试图中
DataView view = new DataView(table);
dg.DataSource = view;
dg.DataBind();