GridView既強大又好用。為了讓它更強大、更好用,我們來寫一個繼承自GridView的控件。
<a href="http://webabcd.blog.51cto.com/1787395/345429" target="_blank">[索引頁]</a>
<a href="http://down.51cto.com/data/101204" target="_blank">[源碼下載下傳]</a>
擴充GridView控件(2) - 複合排序和排序狀态提示
/*正式版的實作 開始*/
介紹
擴充GridView控件:
對多個字段進行複合排序;升序、降序的排序狀态提示
使用方法(設定SmartSorting複合屬性):
AllowSortTip - 是否啟用排序提示
AllowMultiSorting - 是否啟用複合排序
SortAscImageUrl - 升序提示圖檔的URL(不設定則使用預設圖檔)
SortDescImageUrl - 降序提示圖檔的URL(不設定則使用預設圖檔)
SortAscText - 升序提示文本
SortDescText - 降序提示文本
關鍵代碼
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
namespace YYControls.SmartGridViewFunction
{
/// <summary>
/// 擴充功能:複合排序和排序狀态提示
/// </summary>
public class SmartSortingFunction : ExtendFunction
{
/// <summary>
/// 構造函數
/// </summary>
public SmartSortingFunction()
: base()
{
}
/// <param name="sgv">SmartGridView對象</param>
public SmartSortingFunction(SmartGridView sgv)
: base(sgv)
/// 擴充功能的實作
protected override void Execute()
// 開啟SmartGridView的排序功能
// this._sgv.AllowSorting = true;
this._sgv.Sorting += new System.Web.UI.WebControls.GridViewSortEventHandler(_sgv_Sorting);
this._sgv.RowDataBoundCell += new SmartGridView.RowDataBoundCellHandler(_sgv_RowDataBoundCell);
/// SmartGridView的RowDataBoundCell事件
/// <param name="sender"></param>
/// <param name="gvtc"></param>
void _sgv_RowDataBoundCell(object sender, GridViewTableCell gvtc)
if (!String.IsNullOrEmpty(this._sgv.SortExpression)
&& this._sgv.SmartSorting.AllowSortTip
&& gvtc.RowType == DataControlRowType.Header)
{
// 顯示排序狀态提示
DisplaySortTip(this._sgv.SortExpression, gvtc.TableCell);
}
/// Sorting事件
/// <param name="e"></param>
void _sgv_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
// 如果允許複合排序的話,則設定複合排序表達式
if (this._sgv.SmartSorting.AllowMultiSorting)
e.SortExpression = GetSortExpression(e);
/// 獲得複合排序表達式
/// <returns></returns>
protected string GetSortExpression(GridViewSortEventArgs e)
string[] sortColumns = null;
string sortAttribute = this._sgv.SortExpression;
if (!String.IsNullOrEmpty(sortAttribute))
sortColumns = sortAttribute.Split(',');
// 如果原排序表達式有目前排序字段
if (sortAttribute.IndexOf(e.SortExpression) != -1)
// 更新排序表達式
sortAttribute = ModifySortExpression(sortColumns, e.SortExpression);
else
// 為原排序表達式添加新的排序規則(升序)
sortAttribute += String.Concat(",", e.SortExpression, " ASC");
return sortAttribute.Trim(',');
/// 更新排序表達式
/// <param name="sortColumns">各個字段的排序表達式數組</param>
/// <param name="sortExpression">目前需要排序的字段(該字段在sortColumns數組中)</param>
protected string ModifySortExpression(string[] sortColumns, string sortExpression)
// 目前需要排序的字段的升序表達式
string ascSortExpression = String.Concat(sortExpression, " ASC");
// 目前需要排序的字段的降序表達式
string descSortExpression = String.Concat(sortExpression, " DESC");
for (int i = 0; i < sortColumns.Length; i++)
// 各個字段的排序表達式數組中,有目前需要排序的字段的升序表達式
if (ascSortExpression.Equals(sortColumns[i]))
{
// 目前排序字段由升序變為降序
sortColumns[i] = descSortExpression;
break;
}
// 各個字段的排序表達式數組中,有目前需要排序的字段的降序表達式
else if (descSortExpression.Equals(sortColumns[i]))
// 從各個字段的排序表達式數組中,删除目前需要排序的字段
Array.Clear(sortColumns, i, 1);
return String.Join(",", sortColumns).Replace(",,", ",").Trim(',');
/// 顯示排序狀态提示
/// <param name="sortExpression">排序表達式</param>
/// <param name="tc">Header的TableCell</param>
protected void DisplaySortTip(string sortExpression, TableCell tc)
string[] sortColumns = sortExpression.Split(',');
if (tc.Controls.Count > 0 && tc.Controls[0] is LinkButton)
string columnName = ((LinkButton)tc.Controls[0]).CommandArgument;
int sortOrderIndex = Array.FindIndex(sortColumns, delegate(string s) { return s.IndexOf(columnName) != -1; });
// 如果排序表達式中有目前列
if (sortOrderIndex != -1)
// 目前列應該是升序還是降序(區分兩種情況:複合排序和非複合排序)
SortDirection sortDirection = SortDirection.Ascending;
if (this._sgv.SmartSorting.AllowMultiSorting && sortColumns[sortOrderIndex].IndexOf("DESC") != -1)
{
sortDirection = SortDirection.Descending;
}
else if (!this._sgv.SmartSorting.AllowMultiSorting && this._sgv.SortDirection == SortDirection.Descending)
// 排序狀态提示(圖檔位址)
string sortImageUrl = null;
// 排序狀态提示(文本)
string sortText = null;
if (sortDirection == SortDirection.Ascending)
sortText = this._sgv.SmartSorting.SortAscText;
sortImageUrl = this._sgv.SmartSorting.SortAscImageUrl;
if (String.IsNullOrEmpty(sortImageUrl))
{
sortImageUrl =
this._sgv.Page.ClientScript.GetWebResourceUrl
(
this.GetType(),
"YYControls.SmartGridView.Resources.Asc.gif"
);
}
else
sortText = this._sgv.SmartSorting.SortDescText;
sortImageUrl = this._sgv.SmartSorting.SortDescImageUrl;
"YYControls.SmartGridView.Resources.Desc.gif"
// 添加排序狀态提示圖檔
Image imgSortDirection = new Image();
imgSortDirection.ImageUrl = sortImageUrl;
tc.Controls.Add(imgSortDirection);
// 添加排序狀态提示文本
tc.Controls.Add(new LiteralControl(sortText));
if (this._sgv.SmartSorting.AllowMultiSorting)
// 添加排序狀态提示序号
tc.Controls.Add(new LiteralControl((sortOrderIndex + 1).ToString()));
不用資源檔案,又沒有設定排序狀态提示時的邏輯(已經注釋掉了)#region 不用資源檔案,又沒有設定排序狀态提示時的邏輯(已經注釋掉了)
//// 添加排序狀态提示圖示
//Label lblSortDirection = new Label();
//// 被注釋的代碼使用的是webdings,但是FF不支援,是以放棄了
//// lblSortDirection.Font.Name = "webdings";
//// lblSortDirection.EnableTheming = false;
//if (sortDirection == SortDirection.Ascending)
//{
// // lblSortDirection.Text = "5";
// lblSortDirection.Text = "▲";
//}
//else
// // lblSortDirection.Text = "6";
// lblSortDirection.Text = "▼";
//tc.Controls.Add(lblSortDirection);
//if (this._sgv.SmartSorting.AllowMultiSorting)
// // 添加排序狀态提示序号
// tc.Controls.Add(new LiteralControl((sortOrderIndex + 1).ToString()));
#endregion
} // if (sortOrderIndex != -1)
} // if (tc.Controls.Count > 0 && tc.Controls[0] is LinkButton)
}
}
/*正式版的實作 結束*/
<a href="http://webabcd.blog.51cto.com/1787395/345445" target="_blank">未完待續>></a>
本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/345446,如需轉載請自行聯系原作者