2012-11-15 97 views
0

這是我的GV。如何排序GridView

<asp:GridView ID="Grid1" runat="server" AutoGenerateColumns="false" 
      AllowPaging="True" OnPageIndexChanging="Grid1_PageIndexChanging"> 
      <Columns> 
       <asp:BoundField DataField="One" HeaderText="One" /> 
       <asp:BoundField DataField="Two" HeaderText="Two" /> 
       <asp:BoundField DataField="Three" HeaderText="Three" /> 
      </Columns> 
</asp:GridView> 

我使用存儲過程填充GV。

table = PublicClass.Sql_GetTable("usp_GetReportGridView", "NCIU"); 
Grid1.DataSource = table; 
Grid1.DataBind(); 

如何使用列標題進行排序?

回答

2

首先,您需要啓用AllowSorting屬性爲true。啓用後,網格將在每個列的標題中呈現LinkBut​​ton控件。當按鈕被點擊時,網格的SortCommand事件被拋出。這取決於你在代碼中處理這個事件。因爲數據網格始終顯示在它在數據源中發生的相同的順序的數據,典型的邏輯排序的數據源,然後在下面的代碼重新綁定數據到grid.look:

//AllowSorting="True" 
//OnSorting="GridView2_Sorting" 
//SortExpression="name" 

protected void GridView2_Sorting(object sender, GridViewSortEventArgs e) 
{ 
    //to check whether to display in ascending order or descending order 
    if (e.SortExpression.Trim() == this.SortField) 
    this.SortDirection = (this.SortDirection == "D" ? "A" : "D"); 
    else 
    this.SortDirection = "A"; 
    this.SortField = e.SortExpression; 
    TempTable(); 
} 
+0

良好的答案(和多比我更好,我已經刪除了)。 –