Gridview的手动排序事件

时间:2010-10-29 13:47:08

标签: asp.net c#-4.0

请帮助我。我用list daatsource创建了gridview。我想为gridview应用排序事件来对所有列进行排序。

这是我的代码:

protected void grduAdminSerservice_Sorting(object sender,GridViewSortEventArgs e)     {

    DataTable tbl = grduAdminSerservice.DataSource as DataTable;

    if (tbl != null)
    {
        DataView dv = new DataView(tbl);
        dv.Sort = e.SortExpression + "" + getSortDirectionString(e.SortDirection);

        grduAdminSerservice.DataSource = dv;
        grduAdminSerservice.DataBind();
    }

}

private string getSortDirectionString(SortDirection sortDirection)
{
    string newSortDirection = String.Empty;
    if(sortDirection== SortDirection.Ascending)
    {
           newSortDirection = "ASC";
    }
    else
    {
           newSortDirection = "DESC";
    }

     return newSortDirection;
}

........

但表只获取空值。如何将gridview源绑定到表。请帮帮我

1 个答案:

答案 0 :(得分:0)

DataTable tbl = grduAdminSerservice.DataSource as DataTable; 

您有另一种移动数据的方法,但在使用以下代码之前,您需要在绑定gridview时将数据存储在session或viewstate中:

 DataSet ds =(DataSet) Session["GridData"];
 DataTable dt=ds.Tables[0];