9

我似乎无法弄清楚如何使用数据绑定和自定义字段对我的网格视图进行排序。

自定义字段如下所示:

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

它需要一个函数来显示给定类别有多少项目。

数据绑定字段的排序工作完美,但不是自定义字段。我也在寻找一种适用于我所有网格视图的通用方法。

有人可以在正确的方向上帮助我吗?下面是我的完整 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>";
    }
}
4

2 回答 2

16

我有同样的问题,BoundField它应该排序,但TemplateField根本没有排序。

我改变了这个:

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

对此:

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

我删除了HeaderTemplateHeaderTextTemplateField.

于 2012-02-13T13:39:09.093 回答
4

确保在模板字段上指定 SortExpression 属性

<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>
于 2009-10-21T00:55:43.117 回答