2012-04-02 16 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

あなたがこれを読みました。 webcontrols.gridview.allowsorting.aspx – Arion

答えて

0

上の任意の入力は、あなたがそれをMinistry Name呼ばれdatasorceあなたの列を持っていますか。またはMinistryNameと呼ばれています。カラムがMinistryNameの場合その後sortingexpressionは次のようになります。http://msdn.microsoft.com/en-us/library/system.web.ui:

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

Page Enableグリッドでも機能しますか? – sqlnewbie

+0

うわーそれはうまくいくでしょう –

+0

Sunil、ソート式はどうですか?私たちが与えるべきことは、DataFieldNameまたはHeaderName – sqlnewbie