RadGrid.MasterTableView.FilterExpression Exception

1 Answer 64 Views
Filter Grid
Benjamin
Top achievements
Rank 1
Benjamin asked on 07 Sep 2023, 02:38 PM

Hello

i'm stuck with a simple example.

I have a RadGrid with multiple column avec ExcelFilter activated.

When in a column, you have data like 'test AND test' the Filter got no FilterExpression when you select this particular line in  the filter with a tickbox.

If you use the Filter fonction normally (Contain) it work.

 

Here is a simple exemple.

Default.aspx :


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <telerik:RadStyleSheetManager id="RadStyleSheetManager1" runat="server" />
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        <Scripts>
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
        </Scripts>
    </telerik:RadScriptManager>
    <div>
        <telerik:RadGrid ID="GridFile" runat="server"         
            OnNeedDataSource="ContextGrid_NeedDataSource"
            AllowSorting="true"
            AllowFilteringByColumn="True"
            FilterType="HeaderContext"
            EnableHeaderContextMenu="true" 
            EnableHeaderContextFilterMenu="true"
            OnFilterCheckListItemsRequested="RadGrid1_FilterCheckListItemsRequested"  
            FilterMenu-EnableRoundedCorners="false"
            >
             <MasterTableView AutoGenerateColumns="false" TableLayout="Auto"> 
                <Columns>
                    <telerik:GridBoundColumn UniqueName="Creation" DataField="Creation" HeaderText="Creation"   FilterCheckListEnableLoadOnDemand="true"/>
                    <telerik:GridBoundColumn UniqueName="Name" DataField="Name" HeaderText="Name" FilterCheckListEnableLoadOnDemand="true"/>
                 </Columns>
            </MasterTableView>
        </telerik:RadGrid>
    </div>
    </form>
</body>
</html>


And Defaults.aspx.cs 


using System.Data;
using Telerik.Web.UI;


public partial class Default : System.Web.UI.Page 
{
    
    protected void ContextGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        DataTable table = new DataTable();

        GridFile.DataSource = getTable();
    }


    protected void RadGrid1_FilterCheckListItemsRequested(object sender, GridFilterCheckListItemsRequestedEventArgs e)
    {
        string DataField = (e.Column as IGridDataColumn).GetActiveDataField();
        DataTable table = getTable();

        table.DefaultView.Sort = DataField + " asc";

        e.ListBox.DataSource = table.DefaultView.ToTable(true, DataField);
        e.ListBox.DataKeyField = DataField;
        e.ListBox.DataTextField = DataField;
        e.ListBox.DataValueField = DataField;
        e.ListBox.DataBind();
    }

    protected DataTable getTable()
    {
        DataTable table = new DataTable();
        table.Columns.Add("Creation", typeof(string));
        table.Columns.Add("Name", typeof(string));

        int i = 0;
        for (i=0; i < 10; i++)
        {

            table.Rows.Add(
                "Name " + i,
                i + " The AND a test"
                ) ;
        }
        return table;
    }

}

 

Selection and Error in the 2 captures.

Works with OR also.

Any Clues ?

Thinks it's the same error like the quote.

 

Thanks a lot

Benjamin

 

1 Answer, 1 is accepted

Sort by
0
Doncho
Telerik team
answered on 12 Sep 2023, 09:26 AM

Hi Benjamin,

Thank you for the provided information!

RadGrid control considers quotes, "AND", "OR" and other similar meaningful statements in query context as illegal when filtering. This is done with security concerns. Yet, you can modify the collection of illegal strings of the Grid and allow the use of "AND" in the filter value on own risk:

protected void Page_Load(object sender, EventArgs e)
{
    GridFilterFunction.IllegalStrings = new string[] { " LIKE ", " OR ", "\"", ">", "<", "<>", " NULL ", " IS " };//" AND " is removed from the list
    GridFile.EnableLinqExpressions = false;
}

More details on this matter you can find here:

I hope you will find this helpful.

Kind regards,
Doncho
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources
Benjamin
Top achievements
Rank 1
commented on 12 Sep 2023, 09:57 AM | edited

Thanks a lot for this answer, it's clear.

Why do i need to set EnableLinqExpressions to false ?

GridFile.EnableLinqExpressions = false;

Benjamin

Doncho
Telerik team
commented on 12 Sep 2023, 10:33 AM

Hi Benjamin,

In fact, there is no specific need to disable the LinqExpressions in the current case. Please feel free to skip that line and see if the behavior will still be the same.

More details about the filtering mechanisms of RadGrid can be found in the following resources:

Benjamin
Top achievements
Rank 1
commented on 12 Sep 2023, 12:22 PM

Works like a charm now. Thanks a lot!
Tags
Filter Grid
Asked by
Benjamin
Top achievements
Rank 1
Answers by
Doncho
Telerik team
Share this question
or