码迷,mamicode.com
首页 > 编程语言 > 详细

GridView多列排序

时间:2016-06-27 12:18:33      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
  1 public class WebGridView:GridView
  2 {
  3     属性#region 属性
  4     /**//// <summary>
  5     /// 是否启用或者禁止多列排序
  6     /// </summary>
  7     [
  8     Description("是否启用多列排序功能"),
  9     Category("排序"),
 10     DefaultValue("false"),
 11     ]
 12     public bool AllowMultiColumnSorting
 13     {
 14         get
 15         {
 16             object o = ViewState["EnableMultiColumnSorting"];
 17             return (o != null ? (bool)o : false);
 18         }
 19         set
 20         {
 21             AllowSorting = true;
 22             ViewState["EnableMultiColumnSorting"] = value;
 23         }
 24     }
 25     /**//// <summary>
 26     /// 升序时显示图标
 27     /// </summary>
 28     [
 29     Description("升序时显示图标"),
 30     Category("排序"),
 31     Editor("System.Web.UI.Design.UrlEditor", typeof(System.Drawing.Design.UITypeEditor)),
 32     DefaultValue(""),
 33 
 34     ]
 35     public string SortAscImageUrl
 36     {
 37         get
 38         {
 39             object o = ViewState["SortImageAsc"];
 40             return (o != null ? o.ToString() : "");
 41         }
 42         set
 43         {
 44             ViewState["SortImageAsc"] = value;
 45         }
 46     }
 47     /**//// <summary>
 48     /// 降序时显示图标
 49     /// </summary>
 50     [
 51     Description("降序时显示图标"),
 52     Category("排序"),
 53     Editor("System.Web.UI.Design.UrlEditor", typeof(System.Drawing.Design.UITypeEditor)),
 54     DefaultValue(""),
 55     ]
 56     public string SortDescImageUrl
 57     {
 58         get
 59         {
 60             object o = ViewState["SortImageDesc"];
 61             return (o != null ? o.ToString() : "");
 62         }
 63         set
 64         {
 65             ViewState["SortImageDesc"] = value;
 66         }
 67     }
 68     #endregion
 69     重写方法#region 重写方法
 70     protected override void OnSorting(GridViewSortEventArgs e)
 71     {
 72         if (AllowMultiColumnSorting)
 73         {
 74             e.SortExpression = GetSortExpression(e);
 75         }
 76 
 77         base.OnSorting(e);
 78     }
 79     protected override void OnRowCreated(GridViewRowEventArgs e)
 80     {
 81         if (e.Row.RowType == DataControlRowType.Header)
 82         {
 83             if (SortExpression != String.Empty)
 84             {
 85                 DisplaySortOrderImages(SortExpression, e.Row);
 86                 this.CreateRow(0, 0, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
 87             }
 88         }
 89         base.OnRowCreated(e);
 90     }
 91     #endregion
 92     受保护的方法#region 受保护的方法
 93     /**//// <summary>
 94     ///  获取排序表达式
 95     /// </summary>
 96     protected string GetSortExpression(GridViewSortEventArgs e)
 97     {
 98         string[] sortColumns = null;
 99         string sortAttribute = SortExpression;
100 
101         if (sortAttribute != String.Empty)
102         {
103             sortColumns = sortAttribute.Split(",".ToCharArray());
104         }
105         if (sortAttribute.IndexOf(e.SortExpression) > 0 || sortAttribute.StartsWith(e.SortExpression))
106         {
107             sortAttribute = ModifySortExpression(sortColumns, e.SortExpression);
108         }
109         else
110         {
111             sortAttribute += String.Concat(",", e.SortExpression, " ASC ");
112         }
113         return sortAttribute.TrimStart(",".ToCharArray()).TrimEnd(",".ToCharArray());
114 
115     }
116     /**//// <summary>
117     ///  修改排序顺序
118     /// </summary>
119     protected string ModifySortExpression(string[] sortColumns, string sortExpression)
120     {
121         string ascSortExpression = String.Concat(sortExpression, " ASC ");
122         string descSortExpression = String.Concat(sortExpression, " DESC ");
123 
124         for (int i = 0; i < sortColumns.Length; i++)
125         {
126 
127             if (ascSortExpression.Equals(sortColumns[i]))
128             {
129                 sortColumns[i] = descSortExpression;
130             }
131 
132             else if (descSortExpression.Equals(sortColumns[i]))
133             {
134                 Array.Clear(sortColumns, i, 1);
135             }
136         }
137 
138         return String.Join(",", sortColumns).Replace(",,", ",").TrimStart(",".ToCharArray());
139 
140     }
141     /**//// <summary>
142     ///  获取当前的表达式对所选列进行排序
143     /// </summary>
144     protected void SearchSortExpression(string[] sortColumns, string sortColumn, out string sortOrder, out int sortOrderNo)
145     {
146         sortOrder = "";
147         sortOrderNo = -1;
148         for (int i = 0; i < sortColumns.Length; i++)
149         {
150             if (sortColumns[i].StartsWith(sortColumn))
151             {
152                 sortOrderNo = i + 1;
153                 if (AllowMultiColumnSorting)
154                 {
155                     sortOrder = sortColumns[i].Substring(sortColumn.Length).Trim();
156                 }
157                 else
158                 {
159                     sortOrder = ((SortDirection == SortDirection.Ascending) ? "ASC" : "DESC");
160                 }
161             }
162         }
163     }
164     /**//// <summary>
165     ///  绘制升序降序的图片
166     /// </summary>
167     protected void DisplaySortOrderImages(string sortExpression, GridViewRow dgItem)
168     {
169         string[] sortColumns = sortExpression.Split(",".ToCharArray());
170 
171         for (int i = 0; i < dgItem.Cells.Count; i++)
172         {
173             if (dgItem.Cells[i].Controls.Count > 0 && dgItem.Cells[i].Controls[0] is LinkButton)
174             {
175                 string sortOrder;
176                 int sortOrderNo;
177                 string column = ((LinkButton)dgItem.Cells[i].Controls[0]).CommandArgument;
178                 SearchSortExpression(sortColumns, column, out sortOrder, out sortOrderNo);
179                 if (sortOrderNo > 0)
180                 {
181                     string sortImgLoc = (sortOrder.Equals("ASC") ? SortAscImageUrl : SortDescImageUrl);
182 
183                     if (sortImgLoc != String.Empty)
184                     {
185                         Image imgSortDirection = new Image();
186                         imgSortDirection.ImageUrl = sortImgLoc;
187                         dgItem.Cells[i].Controls.Add(imgSortDirection);
188 
189                     }
190                     else
191                     {
192 
193                         if (AllowMultiColumnSorting)
194                         {
195                             Literal litSortSeq = new Literal();
196                             litSortSeq.Text = sortOrderNo.ToString();
197                             dgItem.Cells[i].Controls.Add(litSortSeq);
198 
199                         }
200                     }
201                 }
202             }
203         }
204 
205     }
206     #endregion
207 }
View Code

 

GridView多列排序

标签:

原文地址:http://www.cnblogs.com/ps122/p/5619617.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!