过滤不适用于Telerik:Radgrid控件

时间:2016-12-16 02:59:38

标签: asp.net telerik filtering

您好我正在使用telerik radgrid。我正在添加DataSource并将其重新绑定在cs文件中的代码中。

问题是过滤不适用于radgrid。

任何帮助将不胜感激。

由于

<telerik:RadGrid ID="ShopOrderRadGrid" runat="server" GridLines="None" OnItemDataBound="ShopOrderRedGridRadGrid__onItemDataBound" OnItemCommand="ShopOrderRedGridRadGrid_ItemCommand" OnNeedDataSource="ShopOrderRadGrid_NeedDataSource" AllowFilteringByColumn="True" AllowSorting="True" AutoGenerateColumns="False" AllowPaging="True"
    PageSize="100">
    <MasterTableView AutoGenerateColumns="False" DataKeyNames="ShopOrderId" HierarchyDefaultExpanded="True" CommandItemDisplay="Top" AllowSorting="True" AllowFilteringByColumn="True" AllowCustomSorting="True" ShowFooter="True"> 
        <Columns>
            <telerik:GridEditCommandColumn ButtonType="ImageButton" Reorderable="False" Resizable="False"
                UniqueName="EditColumn" ShowSortIcon="False">
                <HeaderStyle Width="30px" Wrap="False" />
                <ItemStyle Width="30px" />
            </telerik:GridEditCommandColumn>
            <telerik:GridTemplateColumn HeaderText="Print" AllowFiltering="False">
                <ItemTemplate>
                    <asp:Button runat="server" ID="PrintButton" CommandName="Print" Text="Print Order"
                        OnClientClick="openNewWindow();" />
                </ItemTemplate>
                <HeaderStyle Width="100px" Wrap="False" />
                <ItemStyle Width="100px" />
            </telerik:GridTemplateColumn>
            <telerik:GridBoundColumn DataField="SimpleCode" HeaderText="Order Code" DataType="System.String"
                SortExpression="SimpleCode" UniqueName="SimpleCode"
                AllowFiltering="True" ShowFilterIcon="False"
                AutoPostBackOnFilter="True"
                AndCurrentFilterFunction="Contains"
                CurrentFilterFunction="Contains">
            </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn HeaderText="Name" SortExpression="CustomerFirstname"
                AllowFiltering="True" ShowFilterIcon="False"
                AutoPostBackOnFilter="True"
                AndCurrentFilterFunction="Contains"
                CurrentFilterFunction="Contains">
                <ItemTemplate>
                    <asp:Label ID="NameLabel" runat="server"></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>

            <telerik:GridBoundColumn DataField="TotalCostIncludingTax" HeaderText="Total Cost Including Tax"
                SortExpression="TotalCostIncludingTax" ShowFilterIcon="False" UniqueName="TotalCostIncludingTax"
                DataFormatString="{0:C}" />

        </Columns>
    </MasterTableView>
</telerik:RadGrid>

这是此文件的c#代码。

 protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        shopId = new Guid(Session["shopId"].ToString());
    }
    catch (Exception)
    {
        Response.Redirect("~/Shop/Shop_list.aspx");
    }
    // checking while filtering 

    if (StartDate.SelectedDate == null)
    {
        StartDate.SelectedDate = DateTime.Now.Date.AddDays(-14);
    }
    if (FinishDate.SelectedDate == null)
    {
        FinishDate.SelectedDate = DateTime.Now.Date;
    }
    if (!IsPostBack)
    {

        FilterShopOrderData();
    }
}


private void FilterShopOrderData()
{
    IObjectScope scope = ORM.ScopeFactory.GetPerRequestScope(HttpContext.Current);
    DateTime stdate = DateTime.Parse(StartDate.SelectedDate.ToString());
    DateTime endate = DateTime.Parse(FinishDate.SelectedDate.ToString());
    DateTime startOfDay = new DateTime(stdate.Year, stdate.Month, stdate.Day, 00, 00, 00);
    DateTime endOfDay = new DateTime(endate.Year, endate.Month, endate.Day, 23, 59, 59);


    //Rebind RadGrid Of session.
    List<ORM.Shoporder> shopOrders = null;

    if (CheckBoxShowOrderStatus.Checked == true)
    {
        shopOrders = (from shopOrder in scope.Extent<ORM.Shoporder>()
                      where shopOrder.ShopId == shopId && shopOrder.ShoporderStatus.IsComplete == !CheckBoxShowOrderStatus.Checked &&
                            shopOrder.CreateDateTime.Date >= startOfDay && shopOrder.CreateDateTime.Date <= endOfDay 
                            && shopOrder.IsDeleted == false 
                            && (shopOrder.IsReceiptSent == true || shopOrder.IsReceiptSkipped == true || shopOrder.IsPDFSent == true)
                      orderby shopOrder.CreateDateTime descending
                      select shopOrder).ToList();

        SubjectHeadingLabel.Text = DAL.DataClasses.Shop.GetShopNameById(shopId) + ": Incomplete Orders";
    }
    else
    {
        shopOrders = (from shopOrder in scope.Extent<ORM.Shoporder>()
                      where shopOrder.ShopId == shopId &&
                            shopOrder.CreateDateTime.Date >= startOfDay && shopOrder.CreateDateTime.Date <= endOfDay 
                            && shopOrder.IsDeleted == false 
                            && (shopOrder.IsReceiptSent == true || shopOrder.IsReceiptSkipped == true || shopOrder.IsPDFSent == true)
                      orderby shopOrder.CreateDateTime descending
                      select shopOrder).ToList();

        SubjectHeadingLabel.Text = DAL.DataClasses.Shop.GetShopNameById(shopId) + ": All Orders";
    }

    ShopOrderRadGrid.DataSource = shopOrders;
    //ShopOrderRadGrid.DataBind();
    ShopOrderRadGrid.Rebind();
}

protected void ShopOrderRedGridRadGrid_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
    GridDataItem dataItem = e.Item as GridDataItem;

    if (e.CommandName == RadGrid.EditCommandName)
    {
        //Get the PK of the item.
        Guid shopOrderId = new Guid();
        shopOrderId = (Guid)dataItem.GetDataKeyValue("ShopOrderId");
        //store PK in session
        Session["shopOrderId"] = shopOrderId.ToString();
        //redirect to the Order mod page.
        Response.Redirect("~/Shop/Order/Order_mod.aspx");
    }

    if (e.CommandName == RadGrid.InitInsertCommandName)
    {
        //Get the PK of the item.
        //clear the session
        Session["shopOrderId"] = null;
        //redirect to the Order mod page.
        Response.Redirect("~/Shop/Order/Order_mod.aspx");
    }

    //For Delete
    if (e.CommandName == "Delete")
    {
        Guid shopOrderId = Guid.Empty;

        shopOrderId = (Guid)dataItem.GetDataKeyValue("ShopOrderId");
        IObjectScope scope = ORM.ScopeFactory.GetPerRequestScope(HttpContext.Current);

        scope.Transaction.Begin();
        ORM.Shoporder shopOrder = DAL.DataClasses.ShopOrder.GetObjectById(shopOrderId);
        shopOrder.IsDeleted = true;
        scope.Transaction.Commit();
        //ShopOrderRadGrid.Rebind();
        FilterShopOrderData();
    }

    if (e.CommandName == "FilterRadGrid")
    {
        FilterShopOrderData();
    }
}

protected void ShopOrderRedGridRadGrid__onItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = e.Item as GridDataItem;
        Guid shopOrderId = new Guid();
        ImageButton button = item["DeleteColumn"].Controls[0] as ImageButton;
        button.Attributes["onclick"] = "return confirm('Are you sure you want to delete this order? Deleted orders can be restored from Recycle Bin.')";
        shopOrderId = new Guid(item.GetDataKeyValue("ShopOrderId").ToString());

        ORM.Shoporder thisShopOrder = DAL.DataClasses.ShopOrder.GetObjectById(shopOrderId);

        Label NameLabel = (Label)e.Item.FindControl("NameLabel");
        NameLabel.Text = thisShopOrder.CustomerFirstname + " " + thisShopOrder.CustomerLastname;

        ORM.Site thisSite = DAL.DataClasses.Company.GetDefaultSite(thisShopOrder.Shop.CompanyId);

        Button PrintButton = (Button)item.FindControl("PrintButton");
        PrintButton.OnClientClick = "openNewWindow('" + DAL.DataClasses.ShopOrder.GetOrderReceiptURL(shopOrderId) + "'); return false;";

        Label OrderTypeLabel = (Label)e.Item.FindControl("OrderTypeLabel");
        OrderTypeLabel.Text = "Physical Products";

        if (thisShopOrder.IsEmailDelivery)
        {
            OrderTypeLabel.Text = "E-Products";
        }
        if (thisShopOrder.Shopdeliverymethod.IsPhysicalDelivery == true && thisShopOrder.IsEmailDelivery)
        {
            OrderTypeLabel.Text = "Physical & E-Products";
        }
    }
}

protected void ShopOrderRadGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    // ShopOrderRadGrid.Rebind();
    FilterShopOrderData();
}

1 个答案:

答案 0 :(得分:1)

在代码隐藏中应用过滤器。

要在代码中应用过滤器,您需要做三件事:

  1. 设置过滤器过滤器表达式

    RAD_Grid.MasterTableView.FilterExpression = "(Field >= DateTime.Today.AddDays(-14) )" ;
    

    这是网格将过滤的表达式。

  2. 设置列电流过滤器
    这仅适用于列标题中的过滤器显示。这仅适用于显示和将来的过滤器。必须设置或网格将忽略任何过滤器表达式。您无法过滤列中不存在的数据。即使列已隐藏,您也可以进行过滤。

    GridColumn column = RAD_Grid.MasterTableView.GetColumnSafe("Field");
    column.CurrentFilterFunction = GridKnownFunction.GreaterThanOrEqualTo ;
    column.CurrentFilterValue = DateTime.Today.AddDays(-14).ToShortDateString();
    
  3. <强>重新绑定


  4. 如果您需要一个范围或者您有多个过滤器连接/设置它们在表达式(1。)

    如果是初始过滤器,您可以将其设置为aspx。这样就不需要重新绑定了。

      

    EnableLinqExpressions =“true”是文档中以红色显示的重要内容。

      

    注意:

         

    如果使用NeedDataSource事件绑定网格,则可以   在NeedDataSource事件处理程序中设置初始过滤器并省略   调用Rebind方法。设置过滤器的代码必须   仍然被放置在检查它的if语句中   Page.IsPostBack是假的。请注意,这适用于非自动   生成的列。