GridView排序Sorting事件
1、if (!IsPostBack)
{
DBindFilter();
this.gvwData.Attributes.Add("SortExpression", "HouseID");
this.gvwData.Attributes.Add("SortDirection", "ASC");
GridViewDataBind();
chklColumns.Visible = !IsPostBack; //设置“显示字段”的下拉属性
}
else
{
chklColumns.Visible = IsPostBack; //设置“显示字段”的下拉属性
}
2、// 获取GridView排序数据列及排序方向
string sortExpression = this.gvwData.Attributes["SortExpression"];
string sortDirection = this.gvwData.Attributes["SortDirection"];
if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
{
table.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
}
gvwData.DataSource = table.DefaultView;
gvwData.DataBind();
3、//排序
protected void gvwData_Sorting(object sender, GridViewSortEventArgs e)
{
//获取排序数据列
string sortExpression = e.SortExpression.ToString();
//假定为排序方向为“顺序”
string sortDirection = "ASC";
// “ASC”与事件参数获取到的排序方向进行比较,进行GridView排序方向参数的修改
if (sortExpression == this.gvwData.Attributes["SortExpression"])
{
//获得下一次的排序状态
sortDirection = (this.gvwData.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
}
// 重新设定GridView排序数据列及排序方向
this.gvwData.Attributes["SortExpression"] = sortExpression;
this.gvwData.Attributes["SortDirection"] = sortDirection;
this.GridViewDataBind();
}