根据老外的方法,GridVew实现多列排序.

本文介绍了一个自定义 ASP.NET GridView 控件,该控件支持多列排序功能,并允许设置升序和降序的图标路径。通过修改 SortExpression 属性实现排序逻辑,同时在表头显示排序状态。
代碼如下:
ContractedBlock.gif ExpandedBlockStart.gif
  1ContractedBlock.gifExpandedBlockStart.gifUsing directives#region Using directives
  2InBlock.gif
  3InBlock.gifusing System;
  4InBlock.gifusing System.Collections.Generic;
  5InBlock.gifusing System.ComponentModel;
  6InBlock.gifusing System.Text;
  7InBlock.gifusing System.Web.UI;
  8InBlock.gifusing System.Web.UI.WebControls;
  9InBlock.gif
 10ExpandedBlockEnd.gif#endregion

 11None.gif
 12None.gifnamespace GridControl
 13ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 14InBlock.gif 
 15InBlock.gif    public class ctGridView: GridView
 16ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 17ContractedSubBlock.gifExpandedSubBlockStart.gif        Properties#region Properties
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 19InBlock.gif        /// Enable/Disable MultiColumn Sorting.
 20ExpandedSubBlockEnd.gif        /// </summary>

 21InBlock.gif        [
 22InBlock.gif        Description("Whether Sorting On more than one column is enabled"),
 23InBlock.gif        Category("Behavior"),
 24InBlock.gif        DefaultValue("false"),
 25InBlock.gif        ]
 26InBlock.gif        public bool AllowMultiColumnSorting
 27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 28InBlock.gif            get
 29ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 30InBlock.gif                object o = ViewState["EnableMultiColumnSorting"];
 31InBlock.gif                return (o != null ? (bool)o : false);
 32ExpandedSubBlockEnd.gif            }

 33InBlock.gif            set
 34ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 35InBlock.gif                AllowSorting = true;
 36InBlock.gif                ViewState["EnableMultiColumnSorting"= value;
 37ExpandedSubBlockEnd.gif            }

 38ExpandedSubBlockEnd.gif        }

 39ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 40InBlock.gif        /// Get or Set Image location to be used to display Ascending Sort order.
 41ExpandedSubBlockEnd.gif        /// </summary>

 42InBlock.gif        [
 43InBlock.gif        Description("Image to display for Ascending Sort"),
 44InBlock.gif        Category("Misc"),
 45InBlock.gif        Editor("System.Web.UI.Design.UrlEditor",typeof(System.Drawing.Design.UITypeEditor)),
 46InBlock.gif        DefaultValue(""),
 47InBlock.gif        
 48InBlock.gif        ]
 49InBlock.gif        public string SortAscImageUrl
 50ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 51InBlock.gif            get
 52ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 53InBlock.gif                object o = ViewState["SortImageAsc"];
 54InBlock.gif                return (o != null ? o.ToString() : "");
 55ExpandedSubBlockEnd.gif            }

 56InBlock.gif            set
 57ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 58InBlock.gif                ViewState["SortImageAsc"= value;
 59ExpandedSubBlockEnd.gif            }

 60ExpandedSubBlockEnd.gif        }

 61ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 62InBlock.gif        /// Get or Set Image location to be used to display Ascending Sort order.
 63ExpandedSubBlockEnd.gif        /// </summary>

 64InBlock.gif        [
 65InBlock.gif        Description("Image to display for Descending Sort"),
 66InBlock.gif        Category("Misc"),
 67InBlock.gif        Editor("System.Web.UI.Design.UrlEditor",typeof(System.Drawing.Design.UITypeEditor)),
 68InBlock.gif        DefaultValue(""),
 69InBlock.gif        ]
 70InBlock.gif        public string SortDescImageUrl
 71ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 72InBlock.gif            get
 73ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 74InBlock.gif                object o = ViewState["SortImageDesc"];
 75InBlock.gif                return (o != null ? o.ToString() : "");
 76ExpandedSubBlockEnd.gif            }

 77InBlock.gif            set
 78ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 79InBlock.gif                ViewState["SortImageDesc"= value;
 80ExpandedSubBlockEnd.gif            }

 81ExpandedSubBlockEnd.gif        }

 82ExpandedSubBlockEnd.gif        #endregion

 83ContractedSubBlock.gifExpandedSubBlockStart.gif        Life Cycle#region Life Cycle
 84InBlock.gif
 85InBlock.gif
 86InBlock.gif        protected override void OnSorting(GridViewSortEventArgs e)
 87ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 88InBlock.gif            if (AllowMultiColumnSorting)
 89InBlock.gif                e.SortExpression  = GetSortExpression(e);
 90InBlock.gif            
 91InBlock.gif            base.OnSorting(e);
 92ExpandedSubBlockEnd.gif        }

 93InBlock.gif
 94InBlock.gif        protected override void OnRowCreated(GridViewRowEventArgs e)
 95ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 96InBlock.gif            if (e.Row.RowType == DataControlRowType.Header)
 97ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 98InBlock.gif                if (SortExpression != String.Empty)
 99InBlock.gif                    DisplaySortOrderImages(SortExpression, e.Row);
100ExpandedSubBlockEnd.gif            }

101InBlock.gif            base.OnRowCreated(e);
102ExpandedSubBlockEnd.gif        }

103InBlock.gif 
104ExpandedSubBlockEnd.gif        #endregion

105ContractedSubBlock.gifExpandedSubBlockStart.gif        Protected Methods#region Protected Methods
106ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
107InBlock.gif        ///  Get Sort Expression by Looking up the existing Grid View Sort Expression 
108ExpandedSubBlockEnd.gif        /// </summary>

109InBlock.gif        protected string GetSortExpression(GridViewSortEventArgs e)
110ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
111InBlock.gif            string[] sortColumns = null;
112InBlock.gif            string sortAttribute = SortExpression;
113InBlock.gif
114InBlock.gif            //Check to See if we have an existing Sort Order already in the Grid View.    
115InBlock.gif            //If so get the Sort Columns into an array
116InBlock.gif            if (sortAttribute != String.Empty)
117ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
118InBlock.gif                sortColumns = sortAttribute.Split(",".ToCharArray());
119ExpandedSubBlockEnd.gif            }

120InBlock.gif
121InBlock.gif            //if User clicked on the columns in the existing sort sequence.
122InBlock.gif            //Toggle the sort order or remove the column from sort appropriately
123InBlock.gif
124InBlock.gif            if (sortAttribute.IndexOf(e.SortExpression) > 0 || sortAttribute.StartsWith(e.SortExpression))
125InBlock.gif                sortAttribute = ModifySortExpression(sortColumns, e.SortExpression);
126InBlock.gif            else
127InBlock.gif                sortAttribute += String.Concat(",", e.SortExpression, " ASC ");
128InBlock.gif            return sortAttribute.TrimStart(",".ToCharArray()).TrimEnd(",".ToCharArray()) ;
129InBlock.gif
130ExpandedSubBlockEnd.gif        }

131ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
132InBlock.gif        ///  Toggle the sort order or remove the column from sort appropriately
133ExpandedSubBlockEnd.gif        /// </summary>

134InBlock.gif        protected string ModifySortExpression(string[] sortColumns, string sortExpression)
135ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
136InBlock.gif
137InBlock.gif            string ascSortExpression = String.Concat(sortExpression, " ASC ");
138InBlock.gif            string descSortExpression = String.Concat(sortExpression, " DESC ");
139InBlock.gif
140InBlock.gif            for (int i = 0; i < sortColumns.Length; i++)
141ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
142InBlock.gif
143InBlock.gif                if (ascSortExpression.Equals(sortColumns[i]))
144ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
145InBlock.gif                    sortColumns[i] = descSortExpression;
146ExpandedSubBlockEnd.gif                }

147InBlock.gif
148InBlock.gif                else if (descSortExpression.Equals(sortColumns[i]))
149ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
150InBlock.gif                    Array.Clear(sortColumns, i, 1);
151ExpandedSubBlockEnd.gif                }

152ExpandedSubBlockEnd.gif            }

153InBlock.gif
154InBlock.gif            return String.Join(",", sortColumns).Replace(",,"",").TrimStart(",".ToCharArray());
155InBlock.gif
156ExpandedSubBlockEnd.gif        }

157ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
158InBlock.gif        ///  Lookup the Current Sort Expression to determine the Order of a specific item.
159ExpandedSubBlockEnd.gif        /// </summary>

160InBlock.gif        protected void SearchSortExpression(string[] sortColumns, string sortColumn, out string sortOrder, out int sortOrderNo)
161ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
162InBlock.gif            sortOrder = "";
163InBlock.gif            sortOrderNo = -1;
164InBlock.gif            for (int i = 0; i < sortColumns.Length; i++)
165ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
166InBlock.gif                if (sortColumns[i].StartsWith(sortColumn))
167ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
168InBlock.gif                    sortOrderNo = i + 1;
169InBlock.gif                    if (AllowMultiColumnSorting)
170InBlock.gif                        sortOrder = sortColumns[i].Substring(sortColumn.Length).Trim();
171InBlock.gif                    else
172InBlock.gif                        sortOrder = ((SortDirection == SortDirection.Ascending) ? "ASC" : "DESC");
173ExpandedSubBlockEnd.gif                }

174ExpandedSubBlockEnd.gif            }

175ExpandedSubBlockEnd.gif        }

176ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
177InBlock.gif        ///  Display a graphic image for the Sort Order along with the sort sequence no.
178ExpandedSubBlockEnd.gif        /// </summary>

179InBlock.gif        protected void DisplaySortOrderImages(string sortExpression,  GridViewRow dgItem)
180ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
181InBlock.gif            string[] sortColumns = sortExpression.Split(",".ToCharArray());
182InBlock.gif
183InBlock.gif            for (int i = 0; i < dgItem.Cells.Count; i++)
184ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
185InBlock.gif                if (dgItem.Cells[i].Controls.Count > 0 && dgItem.Cells[i].Controls[0is LinkButton)
186ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
187InBlock.gif                    string sortOrder;
188InBlock.gif                    int sortOrderNo;
189InBlock.gif                    string column = ((LinkButton)dgItem.Cells[i].Controls[0]).CommandArgument;
190InBlock.gif                    SearchSortExpression(sortColumns, column, out sortOrder, out sortOrderNo);
191InBlock.gif                    if (sortOrderNo > 0)
192ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
193InBlock.gif                        string sortImgLoc = (sortOrder.Equals("ASC"? SortAscImageUrl : SortDescImageUrl);
194InBlock.gif
195InBlock.gif                        if (sortImgLoc != String.Empty)
196ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
197InBlock.gif                            Image imgSortDirection = new Image();
198InBlock.gif                            imgSortDirection.ImageUrl = sortImgLoc;
199InBlock.gif                            dgItem.Cells[i].Controls.Add(imgSortDirection);
200InBlock.gif                            Label lblSortOrder = new Label();
201InBlock.gif                            lblSortOrder.Font.Size = FontUnit.Small;
202InBlock.gif                            lblSortOrder.Text = sortOrderNo.ToString();
203InBlock.gif                            dgItem.Cells[i].Controls.Add(lblSortOrder);
204InBlock.gif
205ExpandedSubBlockEnd.gif                        }

206InBlock.gif                        else
207ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
208InBlock.gif
209InBlock.gif                            Label lblSortDirection = new Label();
210InBlock.gif                            lblSortDirection.Font.Size = FontUnit.XSmall;
211InBlock.gif                            lblSortDirection.Font.Name = "webdings";
212InBlock.gif                            lblSortDirection.EnableTheming = false;
213InBlock.gif                            lblSortDirection.Text = (sortOrder.Equals("ASC"? "5" : "6");
214InBlock.gif                            dgItem.Cells[i].Controls.Add(lblSortDirection);
215InBlock.gif
216InBlock.gif                            if (AllowMultiColumnSorting)
217ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{
218InBlock.gif                                Literal litSortSeq = new Literal();
219InBlock.gif                                litSortSeq.Text = sortOrderNo.ToString();
220InBlock.gif                                dgItem.Cells[i].Controls.Add(litSortSeq);
221InBlock.gif                                
222ExpandedSubBlockEnd.gif                            }

223InBlock.gif
224InBlock.gif
225ExpandedSubBlockEnd.gif                        }

226InBlock.gif
227InBlock.gif
228InBlock.gif                   
229InBlock.gif
230ExpandedSubBlockEnd.gif                    }

231InBlock.gif
232ExpandedSubBlockEnd.gif                }

233ExpandedSubBlockEnd.gif            }

234InBlock.gif
235ExpandedSubBlockEnd.gif        }

236ExpandedSubBlockEnd.gif        #endregion

237ExpandedSubBlockEnd.gif    }

238ExpandedBlockEnd.gif}

但是感覺很不爽
以下是一些相关的資料
http://aspnet.4guysfromrolla.com/demos/dgExample37.aspx
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconsortingfilteringdatausingdataview.asp
http://dotnetjunkies.com/WebLog/thomasswilliams/archive/2005/11/09/133667.aspx
http://aspalliance.com/666

转载于:https://www.cnblogs.com/cnaspnet/archive/2006/08/25/486275.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值