2012-04-02 51 views
0

ASPX代碼。如何在ASP.NET Gridview控件中啓用排序列

<asp:GridView ID="gidtest" runat="server" AutoGenerateColumns="False" BackColor="White" 
     BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="100%" 
     AllowPaging="True" **AllowSorting="True"** 
     OnPageIndexChanging="gidtest_PageIndexChanging"> 
     <Columns> 
      <asp:BoundField DataField="MinistryName" HeaderText="Name" SortExpression="Ministry Name" 
       ItemStyle-Width="40%" > 
<ItemStyle Width="40%"></ItemStyle> 
      </asp:BoundField> 
     </Columns> 
     <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" /> 
     <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" /> 
     <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" /> 
     <PagerSettings FirstPageText="first" LastPageText="last" Mode="NumericFirstLast" PageButtonCount="2"/> 
     <RowStyle BackColor="White" ForeColor="#330099" /> 
     <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" /> 
     <SortedAscendingCellStyle BackColor="#FEFCEB" /> 
     <SortedAscendingHeaderStyle BackColor="#AF0101" /> 
     <SortedDescendingCellStyle BackColor="#F6F0C0" /> 
     <SortedDescendingHeaderStyle BackColor="#7E0000" /> 
    </asp:GridView> 

的SortExpression不工作,在此

+0

你讀過這一點:http://msdn.microsoft.com/en-us/library/system.web.ui。 webcontrols.gridview.allowsorting.aspx – Arion 2012-04-02 06:52:06

回答

0

任何輸入你在你有一列datasorce被稱爲Ministry Name。或者它叫做MinistryName。因爲如果該列被稱爲MinistryName。然後sortingexpression應該是:

SortExpression="MinistryName" 
0

請處理OnSorting事件

<asp:GridView ID="gidtest" runat="server" AutoGenerateColumns="False" BackColor="White" 
     BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="100%" 
     AllowPaging="True" **AllowSorting="True"** 
     OnPageIndexChanging="gidtest_PageIndexChanging" OnSorting="gidtest_Sorting" 
> 
     <Columns> 
      <asp:BoundField DataField="MinistryName" HeaderText="Name" SortExpression="Ministry Name" 
       ItemStyle-Width="40%" > 
<ItemStyle Width="40%"></ItemStyle> 
      </asp:BoundField> 
     </Columns> 
     <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" /> 
     <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" /> 
     <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" /> 
     <PagerSettings FirstPageText="first" LastPageText="last" Mode="NumericFirstLast" PageButtonCount="2"/> 
     <RowStyle BackColor="White" ForeColor="#330099" /> 
     <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" /> 
     <SortedAscendingCellStyle BackColor="#FEFCEB" /> 
     <SortedAscendingHeaderStyle BackColor="#AF0101" /> 
     <SortedDescendingCellStyle BackColor="#F6F0C0" /> 
     <SortedDescendingHeaderStyle BackColor="#7E0000" /> 
    </asp:GridView> 






protected void gidtest_Sorting(object sender, GridViewSortEventArgs e) 
    { 

     try 
     { 
      //Retrieve the table from the session object. 
      DataTable dt = GetData(); 

      if (dt != null) 
      { 

       //Sort the data. 
       dt.DefaultView.Sort = e.SortExpression + " " + **GetSortDirection**(e.SortExpression); 
       gidtest.DataSource = dt; 
       gidtest.DataBind(); 
      } 
     } 
     catch (Exception ex) 
     { 

      throw ex; 
     } 

    } 




private string GetSortDirection(string column) 
    { 

     // By default, set the sort direction to ascending. 
     string sortDirection = "ASC"; 

     // Retrieve the last column that was sorted. 
     string sortExpression = ViewState["SortExpression"] as string; 

     if (sortExpression != null) 
     { 
      // Check if the same column is being sorted. 
      // Otherwise, the default value can be returned. 
      if (sortExpression == column) 
      { 
       string lastDirection = ViewState["SortDirection"] as string; 
       if ((lastDirection != null) && (lastDirection == "ASC")) 
       { 
        sortDirection = "DESC"; 
       } 
      } 
     } 

     // Save new values in ViewState. 
     ViewState["SortDirection"] = sortDirection; 
     ViewState["SortExpression"] = column; 

     return sortDirection; 
    } 
+0

它也適用於啓用頁面的網格嗎? – sqlnewbie 2012-04-02 07:05:00

+0

yup蘇利它會工作 – 2012-04-02 07:07:32

+0

蘇尼爾,怎麼樣排序表達式?我們應該給它的是DataFieldName或HeaderName – sqlnewbie 2012-04-02 07:24:59