2010-10-29 79 views
0

請幫幫我。我已經使用list daatsource創建了gridview。我想爲gridview應用排序事件來對所有列進行排序。Gridview的手動排序事件

這裏是我的代碼:

保護無效grduAdminSerservice_Sorting(對象發件人,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源代碼綁定到表。請幫我

回答

0
DataTable tbl = grduAdminSerservice.DataSource as DataTable; 

你有另一種方式來移動數據,但是你使用下面的代碼之前,你需要存儲在會話或視圖狀態的數據,當您綁定的GridView:

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