c#
  • asp.net
  • gridview
  • sorting
  • templatefield
  • 2009-10-20 9 views 9 likes 
    9

    Nie mogę znaleźć sposobu sortowania widoku siatki z polami niestandardowymi zarówno dla databound, jak i .Sortowanie w trybie sortowania z niestandardowymi polami szablonu

    Pole zwyczaj wyglądać następująco:

    <asp:Label ID="lblItems" runat="server" Text='<%# GetItems((int)DataBinder.Eval(Container.DataItem, "ObjectCategoryID"))%>' /> 
    

    Wymaga to funkcja, która pokazuje ile pozycja dana kategoria ma.

    Sortowanie dla pól z databoundami działa perfec, ale nie pól niestandardowych. Im również szuka ogólnej metody, która działa dla wszystkich moich odsłon.

    Czy ktoś może mi pomóc we właściwym kierunku, proszę? Poniżej znajduje się mój pełny kod customgrid.

    using System; 
    using System.Data; 
    using System.Configuration; 
    using System.Linq; 
    using System.Web; 
    using System.Web.Security; 
    using System.Web.UI; 
    using System.Web.UI.HtmlControls; 
    using System.Web.UI.WebControls; 
    using System.Web.UI.WebControls.WebParts; 
    
    using System.Xml.Linq; 
    using System.Collections; 
    
    namespace CustomControls 
    { 
        public class CustomGrid : GridView 
        { 
         public CustomGrid() 
         { 
           PageIndexChanging += CustomGrid_PageIndexChanging; 
         }   
    
        private string ConvertSortDirectionToSql(SortDirection sortDirection) 
        { 
         string newSortDirection = String.Empty; 
    
         switch (sortDirection) 
         { 
          case SortDirection.Ascending: 
           newSortDirection = "ASC"; 
           break; 
    
          case SortDirection.Descending: 
           newSortDirection = "DESC"; 
           break; 
         } 
    
         return newSortDirection; 
        } 
    
        protected void CustomGrid_PageIndexChanging(object sender, GridViewPageEventArgs e) 
        { 
         this.PageIndex = e.NewPageIndex; 
         this.DataBind(); 
        } 
    
        protected override void OnSorting(GridViewSortEventArgs e) 
        { 
         DataSet ds = (DataSet)System.Web.HttpContext.Current.Session["Source"]; 
         DataTable dataTable = ds.Tables[0]; 
    
         if (dataTable != null) 
         { 
          DataView dataView = new DataView(dataTable); 
    
    
          if ((string)System.Web.HttpContext.Current.Session["Direction"] == "Asc") 
          { 
           dataView.Sort = e.SortExpression + " " + "ASC"; 
           System.Web.HttpContext.Current.Session["Direction"] = "Desc"; 
          } 
    
          else if ((string)System.Web.HttpContext.Current.Session["Direction"] == "Desc") 
          { 
           dataView.Sort = e.SortExpression + " " + "DESC"; 
           System.Web.HttpContext.Current.Session["Direction"] = "Asc"; 
          } 
    
          else 
          { 
           dataView.Sort = e.SortExpression + " " + "ASC"; 
           System.Web.HttpContext.Current.Session["Direction"] = "Desc"; 
          } 
    
          this.DataSource = dataView; 
          this.DataBind(); 
         } 
        } 
    
        protected override void OnInit(System.EventArgs e) 
        { 
         this.AllowSorting = true; 
         this.AllowPaging = true; 
         this.PagerSettings.Mode = PagerButtons.NumericFirstLast; 
         this.AutoGenerateColumns = false; 
         this.CssClass = "gv"; 
         this.RowStyle.CssClass = "gvRow"; 
         this.AlternatingRowStyle.CssClass = "gvAlternateRow"; 
         this.HeaderStyle.CssClass = "gvHeader"; 
         this.GridLines = GridLines.None; 
         this.PagerStyle.CssClass = "gvPager"; 
         this.EmptyDataText = "<div style=\"width:100%;text-align:left;\">No data found</div>"; 
        } 
    } 
    
    +0

    Czy możesz pokazać kod aspx tego widoku siatki? –

    Odpowiedz

    4

    Upewnij określić właściwość SortExpression na polu szablonu

    <asp:TemplateField HeaderText="Object Category ID" SortExpression="ObjectCategoryID"> 
    <ItemTemplate> 
        <asp:Label ID="lblItems" runat="server" Text='<%# GetItems((int)DataBinder.Eval(Container.DataItem, "ObjectCategoryID"))%>' /> 
    </ItemTemplate> 
    </asp:TemplateField> 
    
    14

    Miałem ten sam problem, BoundField została sortowania tak jak powinno, ale TemplateField nie sort w ogóle.

    zmieniłem to:

    <asp:TemplateField SortExpression="Category"> 
        <HeaderTemplate>Category</HeaderTemplate> 
        <ItemTemplate>A value</ItemTemplate> 
    </asp:TemplateField> 
    

    do tego:

    <asp:TemplateField HeaderText="Category" SortExpression="Category"> 
        <ItemTemplate>A value</ItemTemplate> 
    </asp:TemplateField> 
    

    usunąłem HeaderTemplate i dodaje HeaderText w TemplateField.

    Powiązane problemy