GridView多列排序

时间:2021-08-16 05:25:34
 public class WebGridView:GridView
{
属性#region 属性
/**//// <summary>
/// 是否启用或者禁止多列排序
/// </summary>
[
Description("是否启用多列排序功能"),
Category("排序"),
DefaultValue("false"),
]
public bool AllowMultiColumnSorting
{
get
{
object o = ViewState["EnableMultiColumnSorting"];
return (o != null ? (bool)o : false);
}
set
{
AllowSorting = true;
ViewState["EnableMultiColumnSorting"] = value;
}
}
/**//// <summary>
/// 升序时显示图标
/// </summary>
[
Description("升序时显示图标"),
Category("排序"),
Editor("System.Web.UI.Design.UrlEditor", typeof(System.Drawing.Design.UITypeEditor)),
DefaultValue(""), ]
public string SortAscImageUrl
{
get
{
object o = ViewState["SortImageAsc"];
return (o != null ? o.ToString() : "");
}
set
{
ViewState["SortImageAsc"] = value;
}
}
/**//// <summary>
/// 降序时显示图标
/// </summary>
[
Description("降序时显示图标"),
Category("排序"),
Editor("System.Web.UI.Design.UrlEditor", typeof(System.Drawing.Design.UITypeEditor)),
DefaultValue(""),
]
public string SortDescImageUrl
{
get
{
object o = ViewState["SortImageDesc"];
return (o != null ? o.ToString() : "");
}
set
{
ViewState["SortImageDesc"] = value;
}
}
#endregion
重写方法#region 重写方法
protected override void OnSorting(GridViewSortEventArgs e)
{
if (AllowMultiColumnSorting)
{
e.SortExpression = GetSortExpression(e);
} base.OnSorting(e);
}
protected override void OnRowCreated(GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
if (SortExpression != String.Empty)
{
DisplaySortOrderImages(SortExpression, e.Row);
this.CreateRow(, , DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
}
}
base.OnRowCreated(e);
}
#endregion
受保护的方法#region 受保护的方法
/**//// <summary>
/// 获取排序表达式
/// </summary>
protected string GetSortExpression(GridViewSortEventArgs e)
{
string[] sortColumns = null;
string sortAttribute = SortExpression; if (sortAttribute != String.Empty)
{
sortColumns = sortAttribute.Split(",".ToCharArray());
}
if (sortAttribute.IndexOf(e.SortExpression) > || sortAttribute.StartsWith(e.SortExpression))
{
sortAttribute = ModifySortExpression(sortColumns, e.SortExpression);
}
else
{
sortAttribute += String.Concat(",", e.SortExpression, " ASC ");
}
return sortAttribute.TrimStart(",".ToCharArray()).TrimEnd(",".ToCharArray()); }
/**//// <summary>
/// 修改排序顺序
/// </summary>
protected string ModifySortExpression(string[] sortColumns, string sortExpression)
{
string ascSortExpression = String.Concat(sortExpression, " ASC ");
string descSortExpression = String.Concat(sortExpression, " DESC "); for (int i = ; i < sortColumns.Length; i++)
{ if (ascSortExpression.Equals(sortColumns[i]))
{
sortColumns[i] = descSortExpression;
} else if (descSortExpression.Equals(sortColumns[i]))
{
Array.Clear(sortColumns, i, );
}
} return String.Join(",", sortColumns).Replace(",,", ",").TrimStart(",".ToCharArray()); }
/**//// <summary>
/// 获取当前的表达式对所选列进行排序
/// </summary>
protected void SearchSortExpression(string[] sortColumns, string sortColumn, out string sortOrder, out int sortOrderNo)
{
sortOrder = "";
sortOrderNo = -;
for (int i = ; i < sortColumns.Length; i++)
{
if (sortColumns[i].StartsWith(sortColumn))
{
sortOrderNo = i + ;
if (AllowMultiColumnSorting)
{
sortOrder = sortColumns[i].Substring(sortColumn.Length).Trim();
}
else
{
sortOrder = ((SortDirection == SortDirection.Ascending) ? "ASC" : "DESC");
}
}
}
}
/**//// <summary>
/// 绘制升序降序的图片
/// </summary>
protected void DisplaySortOrderImages(string sortExpression, GridViewRow dgItem)
{
string[] sortColumns = sortExpression.Split(",".ToCharArray()); for (int i = ; i < dgItem.Cells.Count; i++)
{
if (dgItem.Cells[i].Controls.Count > && dgItem.Cells[i].Controls[] is LinkButton)
{
string sortOrder;
int sortOrderNo;
string column = ((LinkButton)dgItem.Cells[i].Controls[]).CommandArgument;
SearchSortExpression(sortColumns, column, out sortOrder, out sortOrderNo);
if (sortOrderNo > )
{
string sortImgLoc = (sortOrder.Equals("ASC") ? SortAscImageUrl : SortDescImageUrl); if (sortImgLoc != String.Empty)
{
Image imgSortDirection = new Image();
imgSortDirection.ImageUrl = sortImgLoc;
dgItem.Cells[i].Controls.Add(imgSortDirection); }
else
{ if (AllowMultiColumnSorting)
{
Literal litSortSeq = new Literal();
litSortSeq.Text = sortOrderNo.ToString();
dgItem.Cells[i].Controls.Add(litSortSeq); }
}
}
}
} }
#endregion
}