GridView自动排序
GridView的功能很强大,可以实现很多的功能,下面我讲下GridView自动排序的实现方法。
/// <summary>
/// GridView排序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
if (GridViewSortExpression.Equals(e.SortExpression))
{
GridViewSortExpression = e.SortExpression;
if (GridViewSortDirection.Equals("ASC"))
{
GridViewSortDirection = "DESC";
this.GridView1.DataSource = SortDataTable(数据源, false);
}
else if (GridViewSortDirection.Equals("DESC"))
{
GridViewSortDirection = "ASC";
this.GridView1.DataSource = SortDataTable(数据源, true);
}
else
{
GridViewSortDirection = "ASC";
this.GridView1.DataSource = SortDataTable(数据源, true);
}
}
else
{
GridViewSortDirection = "ASC";
GridViewSortExpression = e.SortExpression;
this.GridView1.DataSource = SortDataTable(数据源, true);
}
this.GridView1.DataBind();
}
/// <summary>
/// 排序,并保存排序结果
/// </summary>
/// <param name="dataTable"></param>
/// <param name="isPageIndexChanging"></param>
/// <returns></returns>
protected DataView SortDataTable(DataTable dataTable, bool isPageIndexChanging)
{
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
if (GridViewSortExpression != string.Empty)
{
if (isPageIndexChanging)
{
dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, "ASC");
}
else
{
dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, "DESC");
}
//if (Session["FileListTable"] != null)
// Session["FileListTable"] = GetDataTable(dataView);
}
return dataView;
}
else
{
return new DataView();
}
}
/// GridView排序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
if (GridViewSortExpression.Equals(e.SortExpression))
{
GridViewSortExpression = e.SortExpression;
if (GridViewSortDirection.Equals("ASC"))
{
GridViewSortDirection = "DESC";
this.GridView1.DataSource = SortDataTable(数据源, false);
}
else if (GridViewSortDirection.Equals("DESC"))
{
GridViewSortDirection = "ASC";
this.GridView1.DataSource = SortDataTable(数据源, true);
}
else
{
GridViewSortDirection = "ASC";
this.GridView1.DataSource = SortDataTable(数据源, true);
}
}
else
{
GridViewSortDirection = "ASC";
GridViewSortExpression = e.SortExpression;
this.GridView1.DataSource = SortDataTable(数据源, true);
}
this.GridView1.DataBind();
}
/// <summary>
/// 排序,并保存排序结果
/// </summary>
/// <param name="dataTable"></param>
/// <param name="isPageIndexChanging"></param>
/// <returns></returns>
protected DataView SortDataTable(DataTable dataTable, bool isPageIndexChanging)
{
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
if (GridViewSortExpression != string.Empty)
{
if (isPageIndexChanging)
{
dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, "ASC");
}
else
{
dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, "DESC");
}
//if (Session["FileListTable"] != null)
// Session["FileListTable"] = GetDataTable(dataView);
}
return dataView;
}
else
{
return new DataView();
}
}