使用自定义模板字段进行 Gridview 排序

发布于 2024-08-08 04:57:13 字数 3201 浏览 5 评论 0原文

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

自定义字段如下所示:

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

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

数据绑定字段的排序工作正常,但自定义字段则不然。我是 还在寻找适用于我所有网格视图的通用方法。

有人可以帮助我朝正确的方向前进吗?以下是我的完整自定义网格代码。

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>";
    }
}

I can't seem to figure out how to sort my gridview with both databound AND
custom fields.

The custom field look like this:

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

It calls for a function which shows how many item the given category has.

The sorting for the databounded fields work perfec but not the customfields. Im
also looking for a generic method which works for all my gridviews.

Can someone help me in the right direction please? Below is my full customgrid code.

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>";
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以歌曲疗慰 2024-08-15 04:57:13

我遇到了同样的问题,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>

我删除了 HeaderTemplate 并在 TemplateField 中添加了 HeaderText

I had the same issue, the BoundField was sorting as it should, but the TemplateField did not sort at all.

I changed this:

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

To this:

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

I removed the HeaderTemplate and added the HeaderText in the TemplateField.

独闯女儿国 2024-08-15 04:57:13

确保在模板字段上指定 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>

Make sure you specify the SortExpression property on the template field

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文