扩展GridView控件(2) - 复合排序和排序状态提示
2007-01-03 18:55
309 查看
GridView既强大又好用。为了让它更强大、更好用,我们来写一个继承自GridView的控件。
[索引页]
[源码下载]
[align=center]扩展GridView控件(2) - 复合排序和排序状态提示
[/align]
作者:webabcd
/*正式版的实现 开始*/
介绍
扩展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()
{
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="sgv">SmartGridView对象</param>
public SmartSortingFunction(SmartGridView sgv)
: base(sgv)
{
}
/// <summary>
/// 扩展功能的实现
/// </summary>
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);
}
/// <summary>
/// SmartGridView的RowDataBoundCell事件
/// </summary>
/// <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);
}
}
/// <summary>
/// Sorting事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void _sgv_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
{
// 如果允许复合排序的话,则设置复合排序表达式
if (this._sgv.SmartSorting.AllowMultiSorting)
{
e.SortExpression = GetSortExpression(e);
}
}
/// <summary>
/// 获得复合排序表达式
/// </summary>
/// <param name="e"></param>
/// <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(',');
}
/// <summary>
/// 更新排序表达式
/// </summary>
/// <param name="sortColumns">各个字段的排序表达式数组</param>
/// <param name="sortExpression">当前需要排序的字段(该字段在sortColumns数组中)</param>
/// <returns></returns>
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);
break;
}
}
return String.Join(",", sortColumns).Replace(",,", ",").Trim(',');
}
/// <summary>
/// 显示排序状态提示
/// </summary>
/// <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)
{
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;
if (String.IsNullOrEmpty(sortImageUrl))
{
sortImageUrl =
this._sgv.Page.ClientScript.GetWebResourceUrl
(
this.GetType(),
"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)
}
}
}
/*正式版的实现 结束*/
未完待续>>
[索引页]
[源码下载]
[align=center]扩展GridView控件(2) - 复合排序和排序状态提示
[/align]
作者:webabcd
/*正式版的实现 开始*/
介绍
扩展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()
{
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="sgv">SmartGridView对象</param>
public SmartSortingFunction(SmartGridView sgv)
: base(sgv)
{
}
/// <summary>
/// 扩展功能的实现
/// </summary>
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);
}
/// <summary>
/// SmartGridView的RowDataBoundCell事件
/// </summary>
/// <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);
}
}
/// <summary>
/// Sorting事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void _sgv_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
{
// 如果允许复合排序的话,则设置复合排序表达式
if (this._sgv.SmartSorting.AllowMultiSorting)
{
e.SortExpression = GetSortExpression(e);
}
}
/// <summary>
/// 获得复合排序表达式
/// </summary>
/// <param name="e"></param>
/// <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(',');
}
/// <summary>
/// 更新排序表达式
/// </summary>
/// <param name="sortColumns">各个字段的排序表达式数组</param>
/// <param name="sortExpression">当前需要排序的字段(该字段在sortColumns数组中)</param>
/// <returns></returns>
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);
break;
}
}
return String.Join(",", sortColumns).Replace(",,", ",").Trim(',');
}
/// <summary>
/// 显示排序状态提示
/// </summary>
/// <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)
{
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;
if (String.IsNullOrEmpty(sortImageUrl))
{
sortImageUrl =
this._sgv.Page.ClientScript.GetWebResourceUrl
(
this.GetType(),
"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)
}
}
}
/*正式版的实现 结束*/
未完待续>>
相关文章推荐
- 扩展GridView控件(2) - 复合排序和排序状态提示
- 扩展GridView控件(2) - 复合排序和排序状态提示
- 扩展GridView控件(2) - 复合排序和排序状态提示
- 上接扩展GridView控件(2) - 复合排序和排序状态提示
- 扩展GridView控件(2) - 复合排序和排序状态提示
- 扩展GridView控件(2) - 复合排序和排序状态提示
- 关于Gridview的分页和排序以及如何维护控件的状态
- 扩展GridView实现的一个自定义无刷新分页,排序,支持多种数据源的控件TwfGridView
- GridView控件(二)——给字段标题加上排序状态
- 扩展GridView控件以包含一个与排序相关的箭头标记
- 自定义控件(HolyrongGridView)-扩展GridView控件(3):自定义排序
- CList的扩展CGridListEx,可排序的grid表格控件
- 使用RedControls控件RadAjaxPanel、AjaxLoadingPanel实现GridView无刷新翻页和排序
- GridView控件在不使用数据源控件时,实现排序和分页
- 扩展ASP.NET 2.0内置GridView控件之一
- 扩展GridView控件(3) - 根据按钮的CommandName设置其客户端属性
- 扩展GridView控件(10) - 自定义分页样式
- 在GridView分页时,维护CheckBoxes控件的选择状态
- 扩展GridView控件(一)——鼠标经过行时改变行的样式
- 【脚本共享】可以排序的Table控件,扩展较强。