GridView列的排序功能

时间:2020-12-08 13:51:40

首先要给GridView设置三个属性

            GridView4.AllowSorting = true;
            GridView4.Attributes.Add("SortExpression", "ENDDATE");
GridView4.Attributes.Add("SortDirection", "ASC");

然后写Sorting事件

    /// <summary>
/// GridView4的排序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView4_Sorting(object sender, GridViewSortEventArgs e)
{
// 从事件参数获取排序数据列
string sortExpression = e.SortExpression.ToString(); // 假定为排序方向为“顺序”
string sortDirection = "ASC"; // “ASC”与事件参数获取到的排序方向进行比较,进行GridView排序方向参数的修改
if (sortExpression == this.GridView4.Attributes["SortExpression"])
{
//获得下一次的排序状态
sortDirection = (this.GridView4.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
} // 重新设定GridView排序数据列及排序方向
this.GridView4.Attributes["SortExpression"] = sortExpression;
this.GridView4.Attributes["SortDirection"] = sortDirection;      //获取展示数据
DataTable table = SocketInfo.GetStatusInfo(sortExpression, sortDirection);
GridView4.DataSource = table;
GridView4.DataBind();
GridView4.Visible = true; }