-
根据老外的方法,GridVew实现多列排序.
代碼如下:
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
1![](/Images/OutliningIndicators/ContractedBlock.gif)
Using directives#region Using directives
2![](/Images/OutliningIndicators/InBlock.gif)
3
using System;
4
using System.Collections.Generic;
5
using System.ComponentModel;
6
using System.Text;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9![](/Images/OutliningIndicators/InBlock.gif)
10
#endregion
11![](/Images/OutliningIndicators/None.gif)
12
namespace GridControl
13![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
14
15
public class ctGridView: GridView
16![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
17![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Properties#region Properties
18![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
19
/// Enable/Disable MultiColumn Sorting.
20
/// </summary>
21
[
22
Description("Whether Sorting On more than one column is enabled"),
23
Category("Behavior"),
24
DefaultValue("false"),
25
]
26
public bool AllowMultiColumnSorting
27![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
28
get
29![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
30
object o = ViewState["EnableMultiColumnSorting"];
31
return (o != null ? (bool)o : false);
32
}
33
set
34![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
35
AllowSorting = true;
36
ViewState["EnableMultiColumnSorting"] = value;
37
}
38
}
39![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
40
/// Get or Set Image location to be used to display Ascending Sort order.
41
/// </summary>
42
[
43
Description("Image to display for Ascending Sort"),
44
Category("Misc"),
45
Editor("System.Web.UI.Design.UrlEditor",typeof(System.Drawing.Design.UITypeEditor)),
46
DefaultValue(""),
47
48
]
49
public string SortAscImageUrl
50![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
51
get
52![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
53
object o = ViewState["SortImageAsc"];
54
return (o != null ? o.ToString() : "");
55
}
56
set
57![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
58
ViewState["SortImageAsc"] = value;
59
}
60
}
61![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
62
/// Get or Set Image location to be used to display Ascending Sort order.
63
/// </summary>
64
[
65
Description("Image to display for Descending Sort"),
66
Category("Misc"),
67
Editor("System.Web.UI.Design.UrlEditor",typeof(System.Drawing.Design.UITypeEditor)),
68
DefaultValue(""),
69
]
70
public string SortDescImageUrl
71![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
72
get
73![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
74
object o = ViewState["SortImageDesc"];
75
return (o != null ? o.ToString() : "");
76
}
77
set
78![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
79
ViewState["SortImageDesc"] = value;
80
}
81
}
82
#endregion
83![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Life Cycle#region Life Cycle
84![](/Images/OutliningIndicators/InBlock.gif)
85![](/Images/OutliningIndicators/InBlock.gif)
86
protected override void OnSorting(GridViewSortEventArgs e)
87![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
88
if (AllowMultiColumnSorting)
89
e.SortExpression = GetSortExpression(e);
90
91
base.OnSorting(e);
92
}
93![](/Images/OutliningIndicators/InBlock.gif)
94
protected override void OnRowCreated(GridViewRowEventArgs e)
95![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
96
if (e.Row.RowType == DataControlRowType.Header)
97![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
98
if (SortExpression != String.Empty)
99
DisplaySortOrderImages(SortExpression, e.Row);
100
}
101
base.OnRowCreated(e);
102
}
103
104
#endregion
105![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Protected Methods#region Protected Methods
106![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
107
/// Get Sort Expression by Looking up the existing Grid View Sort Expression
108
/// </summary>
109
protected string GetSortExpression(GridViewSortEventArgs e)
110![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
111
string[] sortColumns = null;
112
string sortAttribute = SortExpression;
113![](/Images/OutliningIndicators/InBlock.gif)
114
//Check to See if we have an existing Sort Order already in the Grid View.
115
//If so get the Sort Columns into an array
116
if (sortAttribute != String.Empty)
117![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
118
sortColumns = sortAttribute.Split(",".ToCharArray());
119
}
120![](/Images/OutliningIndicators/InBlock.gif)
121
//if User clicked on the columns in the existing sort sequence.
122
//Toggle the sort order or remove the column from sort appropriately
123![](/Images/OutliningIndicators/InBlock.gif)
124
if (sortAttribute.IndexOf(e.SortExpression) > 0 || sortAttribute.StartsWith(e.SortExpression))
125
sortAttribute = ModifySortExpression(sortColumns, e.SortExpression);
126
else
127
sortAttribute += String.Concat(",", e.SortExpression, " ASC ");
128
return sortAttribute.TrimStart(",".ToCharArray()).TrimEnd(",".ToCharArray()) ;
129![](/Images/OutliningIndicators/InBlock.gif)
130
}
131![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
132
/// Toggle the sort order or remove the column from sort appropriately
133
/// </summary>
134
protected string ModifySortExpression(string[] sortColumns, string sortExpression)
135![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
136![](/Images/OutliningIndicators/InBlock.gif)
137
string ascSortExpression = String.Concat(sortExpression, " ASC ");
138
string descSortExpression = String.Concat(sortExpression, " DESC ");
139![](/Images/OutliningIndicators/InBlock.gif)
140
for (int i = 0; i < sortColumns.Length; i++)
141![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
142![](/Images/OutliningIndicators/InBlock.gif)
143
if (ascSortExpression.Equals(sortColumns[i]))
144![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
145
sortColumns[i] = descSortExpression;
146
}
147![](/Images/OutliningIndicators/InBlock.gif)
148
else if (descSortExpression.Equals(sortColumns[i]))
149![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
150
Array.Clear(sortColumns, i, 1);
151
}
152
}
153![](/Images/OutliningIndicators/InBlock.gif)
154
return String.Join(",", sortColumns).Replace(",,", ",").TrimStart(",".ToCharArray());
155![](/Images/OutliningIndicators/InBlock.gif)
156
}
157![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
158
/// Lookup the Current Sort Expression to determine the Order of a specific item.
159
/// </summary>
160
protected void SearchSortExpression(string[] sortColumns, string sortColumn, out string sortOrder, out int sortOrderNo)
161![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
162
sortOrder = "";
163
sortOrderNo = -1;
164
for (int i = 0; i < sortColumns.Length; i++)
165![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
166
if (sortColumns[i].StartsWith(sortColumn))
167![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
168
sortOrderNo = i + 1;
169
if (AllowMultiColumnSorting)
170
sortOrder = sortColumns[i].Substring(sortColumn.Length).Trim();
171
else
172
sortOrder = ((SortDirection == SortDirection.Ascending) ? "ASC" : "DESC");
173
}
174
}
175
}
176![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
177
/// Display a graphic image for the Sort Order along with the sort sequence no.
178
/// </summary>
179
protected void DisplaySortOrderImages(string sortExpression, GridViewRow dgItem)
180![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
181
string[] sortColumns = sortExpression.Split(",".ToCharArray());
182![](/Images/OutliningIndicators/InBlock.gif)
183
for (int i = 0; i < dgItem.Cells.Count; i++)
184![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
185
if (dgItem.Cells[i].Controls.Count > 0 && dgItem.Cells[i].Controls[0] is LinkButton)
186![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
187
string sortOrder;
188
int sortOrderNo;
189
string column = ((LinkButton)dgItem.Cells[i].Controls[0]).CommandArgument;
190
SearchSortExpression(sortColumns, column, out sortOrder, out sortOrderNo);
191
if (sortOrderNo > 0)
192![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
193
string sortImgLoc = (sortOrder.Equals("ASC") ? SortAscImageUrl : SortDescImageUrl);
194![](/Images/OutliningIndicators/InBlock.gif)
195
if (sortImgLoc != String.Empty)
196![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
197
Image imgSortDirection = new Image();
198
imgSortDirection.ImageUrl = sortImgLoc;
199
dgItem.Cells[i].Controls.Add(imgSortDirection);
200
Label lblSortOrder = new Label();
201
lblSortOrder.Font.Size = FontUnit.Small;
202
lblSortOrder.Text = sortOrderNo.ToString();
203
dgItem.Cells[i].Controls.Add(lblSortOrder);
204![](/Images/OutliningIndicators/InBlock.gif)
205
}
206
else
207![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
208![](/Images/OutliningIndicators/InBlock.gif)
209
Label lblSortDirection = new Label();
210
lblSortDirection.Font.Size = FontUnit.XSmall;
211
lblSortDirection.Font.Name = "webdings";
212
lblSortDirection.EnableTheming = false;
213
lblSortDirection.Text = (sortOrder.Equals("ASC") ? "5" : "6");
214
dgItem.Cells[i].Controls.Add(lblSortDirection);
215![](/Images/OutliningIndicators/InBlock.gif)
216
if (AllowMultiColumnSorting)
217![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
218
Literal litSortSeq = new Literal();
219
litSortSeq.Text = sortOrderNo.ToString();
220
dgItem.Cells[i].Controls.Add(litSortSeq);
221
222
}
223![](/Images/OutliningIndicators/InBlock.gif)
224![](/Images/OutliningIndicators/InBlock.gif)
225
}
226![](/Images/OutliningIndicators/InBlock.gif)
227![](/Images/OutliningIndicators/InBlock.gif)
228
229![](/Images/OutliningIndicators/InBlock.gif)
230
}
231![](/Images/OutliningIndicators/InBlock.gif)
232
}
233
}
234![](/Images/OutliningIndicators/InBlock.gif)
235
}
236
#endregion
237
}
238
}
但是感覺很不爽
以下是一些相关的資料
http://aspnet.4guysfromrolla.com/demos/dgExample37.aspxhttp://msdn.microsoft.com/library/en-us/cpguide/html/cpconsortingfilteringdatausingdataview.asphttp://dotnetjunkies.com/WebLog/thomasswilliams/archive/2005/11/09/133667.aspxhttp://aspalliance.com/666
-
相关阅读:
Backbone源码解析(六):观察者模式应用
NodeJs 开发微信公众号(五)真实环境部署
NodeJs 开发微信公众号(四)微信网页授权
NodeJs 开发微信公众号(三)微信事件交互
NodeJs 开发微信公众号(二)测试环境部署
NodeJs 开发微信公众号(一)准备工作
Css 动画的回调
GIT常用命令笔记
论如何在手机端web前端实现自定义原生控件的样式
Box-sizing:小身材,大拳头!
-
原文地址:https://www.cnblogs.com/cnaspnet/p/486275.html
Copyright © 2020-2023
润新知