2012-06-11 32 views
4

我有一個asp:GridView控制,我已經設置AllowSorting="True"屬性上:如何讓asp:GridView排序?

<asp:GridView ID="gridUsers" runat="server" PageSize="100" ShowHeaderWhenEmpty="True" 
    Width="100%" AllowSorting="True" onrowcreated="gridUsers_RowCreated" 
    onsorting="gridUsers_Sorting"> 
</asp:GridView> 

在設計時,網格看起來排序:

enter image description here

但在運行時只有中間一列是可分揀:

enter image description here

如何在ASP.NET中對asp:GridView進行排序?


注意:該asp:GridViewAllowSorting需要一個Sorting事件處理程序存在:

protected void gridUsers_Sorting(object sender, GridViewSortEventArgs e) 
{ 
    //asp:GridView will throw an exception if a Sorting event handler isn't present 
} 

更新:我意識到什麼特別之處說明列。它是顯示名稱從數據庫原樣顯示的唯一列,原樣爲。其餘列i have to fix the display name to be presentable

protected void gridUsers_RowCreated(object sender, GridViewRowEventArgs e) 
{ 
    e.Row.Cells[0].Visible = false; //UserGUID 
    e.Row.Cells[1].Text = "User name"; 
    e.Row.Cells[2].Text = "Full name"; 
    //3=Description 
    e.Row.Cells[4].Text = "E-mail"; 
    e.Row.Cells[5].Text = "Active"; 
     e.Row.Cells[5].Visible = false; 
    e.Row.Cells[6].Text = "Account type"; 
} 

現在我只需要找出棘手的部分;並使列可以排序。

回答

5

這個代碼一定會幫助你:

在GridView,使屬性AllowSorting = 「true」 和GridView中的列給出這樣

<asp:BoundField DataField="UserName" HeaderText="User Name" SortExpression="UserName" /> 
<asp:BoundField DataField="FullName" HeaderText="Full Name" SortExpression="FullName" /> 

並且還使用以下編碼:

protected void gv_Sorting(object sender, GridViewSortEventArgs e) 
{ 
    try 
    { 
     string sortExpression = e.SortExpression; 
     ViewState["z_sortexpresion"] = e.SortExpression; 
     if (GridViewSortDirection == SortDirection.Ascending) 
     { 
      GridViewSortDirection = SortDirection.Descending; 
      SortGridView(sortExpression, "DESC"); 
     } 
     else 
     { 
      GridViewSortDirection = SortDirection.Ascending; 
      SortGridView(sortExpression, "ASC"); 
     } 
    } 
    catch (Exception ex) 
    { 
     return ex.Message.ToString(); 
    } 
} 
public SortDirection GridViewSortDirection 
{ 
    get 
    { 
     if (ViewState["sortDirection"] == null) 
      ViewState["sortDirection"] = SortDirection.Ascending; 
     return (SortDirection)ViewState["sortDirection"]; 
    } 
    set 
    { 
     ViewState["sortDirection"] = value; 
    } 

} 
private void SortGridView(string sortExpression, string direction) 
{ 
    DTSorting = new DataView(DTSorting, "", sortExpression + " " + direction, DataViewRowState.CurrentRows).ToTable(); 
    gv.DataSource = DTSorting; 
    gv.DataBind(); 
} 
public DataTable DTSorting 
{ 
    get 
    { 
     if (ViewState["Sorting"] != null) 
     { 
      return (DataTable)ViewState["Sorting"]; 
     } 
     else 
      return null; 
    } 
    set 
    { 
     ViewState["Sorting"] = value; 
    } 
} 

這裏,考慮「DTSorting」是您綁定GridView「gv」的DataTable。

+0

我不得不重新設計所有使用「早期」綁定,而不是「遲到」綁定(因此在答案中使用'asp:BoundField')。 GridView扼流器如果在運行時綁定它。 –

1
protected void gridUsers_Sorting(object sender, GridViewSortEventArgs e) 
{ 
    DataTable dt = Session["SortedTable"] as DataTable; 

    if (dt != null) 
    { 

     //Sort the data. 
     dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression); 
     gridUsers.DataSource = Session["SortTable"]; 
     gridUsers.DataBind(); 
    } 
} 

從這個msdn example改編: