这几天一直在自测项目,发现一些细节问题,这里再记录一个GridView排序状态的问题。其实算不上问题,只是我自己以前没注意过这种情况,还是经验少。
GridView的Sorting方法在你点击某个Column的时候触发,而如果GridView支持分页,选择分页或者调整分页大小后,GridView会重新Bind,此时会变成默认的排序,不再是之前选中的某个栏位的排序规则。这里其实已经可以看出来了,就是GridView重新绑定的时候,没有获取到当前的排序规则,解决办法也很直观,就是把之前的排序规则保存起来,每次GridView执行Bind的时候,都加上排序规则。
上代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#region GridView的排序
///<summary>
/// GridView的排序
///</summary>
///<param name="sender"></param>
///<param name="e"></param>
protected void gdvFiles_Sorting(object sender, GridViewSortEventArgs e)
{
try
{
//如果已经存了SortDirection,则已经有过排序
if (ViewState["SortExpression"] != null && ViewState["SortDirection"] != null)
{
DataPickerEvent.GridViewSorting(this.GetViewStateData<DataTable>("SourceTable" + DocTypeID), gdvFiles, e, GetViewStateData<string>("SortExpression"), GetViewStateData<string>("SortDirection"));
}
else
{
DataPickerEvent.GridViewSorting(this.GetViewStateData<DataTable>("SourceTable" + DocTypeID), gdvFiles, e);
}
SetViewStateData("SortDirection", gdvFiles.Attributes["SortDirection"]);
SetViewStateData("SortExpression", gdvFiles.Attributes["SortExpression"]);
}
catch (Exception ex)
{
string message = String.Format("SourceFrom gdvFiles_Sorting Error:" + ex.Message);
}
}
#endregion
这个是GridView的Sorting方法,再看一下BindGridView的方法,就知道了:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#region BindGridView
///<summary>
/// 绑定GridView
///<param name="avaliable">是否显示UnAvaliable的记录</param>
///</summary>
private void BindGridView(bool avaliable)
{
//获取维度信息
List<DocAttributeEntity> attributeList = GetViewStateData<List<DocAttributeEntity>>("AttributeList" + DocTypeID);
if (ViewState["SourceTable" + DocTypeID]!=null)
{
DynamicShow_gdvFiles(attributeList);
DataTable dataSource = GetViewStateData<DataTable>("SourceTable" + DocTypeID);
DataTable tableSource = dataSource.Clone();
if (!avaliable)
{
DataRow[] drs = dataSource.Select("File_Status='" + EnumDataHelper.GetEnumString<FileStatus>(FileStatus.Available) + "'");
foreach (DataRow dr in drs)
{
tableSource.ImportRow(dr);
}
if (tableSource.Rows.Count > 0)
{
DataView dv = tableSource.DefaultView;
if (ViewState["SortExpression"] != null && ViewState["SortDirection"] != null)
{
string strSortExpression = ViewState["SortExpression"].ToString();
string strSortDirection = ViewState["SortDirection"].ToString();
dv.Sort = strSortExpression + "" + strSortDirection;
}
this.gdvFiles.DataSource = dv;
this.gdvFiles.DataBind();
this.gdvFiles.Visible = true;
this.divNoSearchResult.Visible = false;
}
else
{
this.gdvFiles.Visible = false;
this.divNoSearchResult.Visible = true;
}
}
else
{
if (dataSource.Rows.Count > 0)
{
DataView dv = tableSource.DefaultView;
if (ViewState["SortExpression"] != null && ViewState["SortDirection"] != null)
{
string strSortExpression = ViewState["SortExpression"].ToString();
string strSortDirection = ViewState["SortDirection"].ToString();
dv.Sort = strSortExpression + " " + strSortDirection;
}
this.gdvFiles.DataSource = dv;
this.gdvFiles.DataBind();
this.gdvFiles.Visible = true;
this.divNoSearchResult.Visible = false;
}
else
{
this.gdvFiles.Visible = false;
this.divNoSearchResult.Visible = true;
}
}
}
}
#endregion