somesogoood

Thursday, February 26, 2009

GridView sort function implementation

GridView sort function implementation
Since dataset does not support sorting, so the dataset bound to the GridView can not be used after the GridView sorting capabilities, required to manually sort Sorting event of GridView implementation, through the DataView's Sort feature to sort the GridView, the following is an example
//********************************************************************************
//处理GridView的排序事件
protected void grv_Messenge_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;
if (GridViewSortDirection == SortDirection.Ascending) //设置排序方向
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, " DESC");
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, " ASC");
}
}

//*******************************************************************************
/*
* 函数名:SortGridView,即对GridView进行排序
* 创建时间:2007年11月7日
* 功能描述:自定义GridView的排序方法,通过DataView中的排序方法对GridView的数据进行排序
* 输入参数:用于排序的关联表达式,排序的方向(升序或降序)
* 使用示例:SortGridView( sortExpression, "DESC")
* 返回值说明:无返回值
*/
private void SortGridView(string sortExpression, string direction)
{
DataSet ds = GetData(); //查找数据源
DataTable dt = ds.Tables[0];

DataView dv = new DataView(dt);
dv.Sort = sortExpression + direction;
grv_Messenge.DataSource = dv; //将DataView绑定到GridView上
grv_Messenge.DataBind();

}

Inside the GridView to be set up to sort out the SortExpression, and the opening can be a sort

No comments: