该怎么可以点击表头所有页都排序,换页也没有问题?
protected void dgvRequests_Sorting(object sender, GridViewSortEventArgs e)
{
// 从事件参数获取排序数据列
string sortExpression = e.SortExpression.ToString();
// 假定为排序方向为“顺序”
string sortDirection = "ASC";
// “ASC”与事件参数获取到的排序方向进行比较,进行GridView排序方向参数的修改
if (sortExpression == this.dgvRequests.Attributes["SortExpression"])
{
//获得下一次的排序状态
sortDirection =
(this.dgvRequests.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
}
// 重新设定GridView排序数据列及排序方向
this.dgvRequests.Attributes["SortExpression"] = sortExpression;
this.dgvRequests.Attributes["SortDirection"] = sortDirection;
this.bindGrid();
HighLightRows(Convert.ToDouble(this.lblCtrTarget.Text));
}
/// <summary>
/// 绑定到GridView
/// </summary>
private void bindGrid()
{
// 获取GridView排序数据列及排序方向
string sortExpression = this.dgvRequests.Attributes["SortExpression"];
string sortDirection = this.dgvRequests.Attributes["SortDirection"];
// 调用业务数据获取方法
DataTable dtBind = ViewState["table"] as DataTable;
// 根据GridView排序数据列及排序方向设置显示的默认数据视图
if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
{
string sort = string.Format("{0} {1}", sortExpression, sortDirection);
dtBind.DefaultView.Sort = sort;
}
// GridView绑定并显示数据
this.dgvRequests.DataSource = dtBind;
this.dgvRequests.DataBind();
}