This is a migrated thread and some comments may be shown as answers.

Filtering NumericColumn with Contains

18 Answers 543 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mahdi
Top achievements
Rank 1
Mahdi asked on 08 Oct 2013, 01:11 PM
Hello.
In my Project i have a Grid which has some columns .Some of them are Numeric , but I need enable filtering on this Column.when I Enable ,I find out There is No 'Contains' item in the Filter List.I need to do it.I need Contains for NumericColumns .Is there any way To Enable 'Contains' for this Kind of Columns Or if not, Can I write some codes For This scenario and How?

18 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 09 Oct 2013, 04:53 AM
Hello,

Please try with the below code snippet.

<telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID"
 DataType="System.String"></telerik:GridBoundColumn>

DataType="system.String"
May be after setting this DataType it might be possible that sorting is not working perfectly.

Thanks,
Jayesh Goyani
0
Mahdi
Top achievements
Rank 1
answered on 09 Oct 2013, 06:41 AM
Hello Jayesh Goyani
Thanks for your Reply.when I use DataType="System.String" , It shows 'Contains' and other Items But This Contains DOES NOT work on NumericColumns. Nothing Happens after Clicking on it.
0
Eyup
Telerik team
answered on 11 Oct 2013, 11:20 AM
Hi Mahdi,

On my side the suggested approach works exactly as requested:
<telerik:GridBoundColumn ... DataType="System.String">

Alternatively, you can use the following configuration to achieve the requested functionality:
<telerik:GridTemplateColumn UniqueName="NumericFieldName" HeaderText="Header"
    DataField="Freight" DataType="System.String">
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Freight") %>'></asp:Label>
    </ItemTemplate>
</telerik:GridTemplateColumn>

Hope this helps. Please give it a try and let me know if it works for you.

Regards,
Eyup
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Mahdi
Top achievements
Rank 1
answered on 12 Oct 2013, 06:35 AM
Hello Eyup
Thank you for Reply
When I Use  DataType="System.string" ,It shows me Some Items Like 'Contains','StartWith','EndWith' ,.... .It Shows All Of items BUT this Items Doesn't Work for Integer.I mean When I click On This Items Nothing Happens.just Items like 'GreaterThan','LessThan','EqualTo' ,... Work for Integer . I tried Your Code But it acts Like this.
It is My Code  :


<
telerik:GridNumericColumn CurrentFilterFunction="equalto" DataField="ContractNumber"

FilterControlWidth="100px" HeaderStyle-Width="300px" UniqueName="ContractNumber"

HeaderText="شماره قرارداد" ShowFilterIcon="true">

<HeaderStyle Width="300px" />

</telerik:GridNumericColumn>

I Changed GridNumericColumn to GridBoundColumn and GridTemplateColumn, But My Problem Exists yet.
I don't Know What My Problem is.

 

0
Eyup
Telerik team
answered on 17 Oct 2013, 07:13 AM
Hi Mahdi,

I have prepared a sample RadGrid web site to demonstrate that the suggested approach works as expected. Can you please run the attached application and try to determine the crucial differences between our projects?

Looking forward to your reply.

Regards,
Eyup
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Vasssek
Top achievements
Rank 1
answered on 05 Dec 2013, 09:10 AM

Hi Eyup,


How can I filter Integer GridBoundColumn as string when EnableLinqExpressions="false" ? Your example didn't work...



Thank you



Vasssek



0
Shinu
Top achievements
Rank 2
answered on 05 Dec 2013, 10:27 AM
Hi Vasssek,

You can set the DataType="System.String" for the BoundColumn that contains a NumericField. Please provide your code snippet if this doesn't help.

ASPX:
<telerik:GridBoundColumn UniqueName="Number" DataType="System.String" DataField="Number" HeaderText="Number">
</telerik:GridBoundColumn>

Thanks,
Shinu
0
Vasssek
Top achievements
Rank 1
answered on 05 Dec 2013, 10:43 AM

Hi Shinu,


I have tried above example from Eyup, where I added EnableLinqExpressions="false" parameter. It didn't work either. It shows all rows instead of filtered ...



My DLLs version: 2013.3.1126.40



Here is the ASPX declaration:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RadGridFilterNumericAsString.aspx.cs" Inherits="RadGridFilterNumericAsString" %>
 
<!DOCTYPE html>
 
<head runat="server">
    <title></title>
</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>
        <script type="text/javascript">
            //Put your JavaScript code here.
        </script>
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        </telerik:RadAjaxManager>
        <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" CellSpacing="0" EnableLinqExpressions="false"
            DataSourceID="SqlDataSource1" GridLines="None" Width="1300px" AllowFilteringByColumn="true">
            <MasterTableView DataSourceID="SqlDataSource1" AutoGenerateColumns="False"
                DataKeyNames="OrderID">
                <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
                <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
                    <HeaderStyle Width="20px"></HeaderStyle>
                </RowIndicatorColumn>
                <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
                    <HeaderStyle Width="20px"></HeaderStyle>
                </ExpandCollapseColumn>
                <Columns>
                    <telerik:GridBoundColumn DataField="OrderID" DataType="System.Int32"
                        FilterControlAltText="Filter OrderID column" HeaderText="OrderID"
                        ReadOnly="True" SortExpression="OrderID" UniqueName="OrderID">
                    </telerik:GridBoundColumn>
                    <telerik:GridDateTimeColumn DataField="OrderDate" DataType="System.DateTime"
                        FilterControlAltText="Filter OrderDate column" HeaderText="OrderDate"
                        SortExpression="OrderDate" UniqueName="OrderDate">
                    </telerik:GridDateTimeColumn>
                    <telerik:GridNumericColumn DataField="Freight" DataType="System.Decimal"
                        FilterControlAltText="Filter Freight column" HeaderText="Filtered As Numeric"
                        SortExpression="Freight" UniqueName="Freight1">
                    </telerik:GridNumericColumn>
                    <telerik:GridBoundColumn DataField="Freight" DataType="System.String"
                        FilterControlAltText="Filter Freight column" HeaderText="Filtered As String"
                        SortExpression="Freight" UniqueName="Freight2">
                    </telerik:GridBoundColumn>
                    <telerik:GridTemplateColumn UniqueName="Freight3" HeaderText="Filtered As String 2"
                        DataField="Freight" DataType="System.String">
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("Freight") %>'></asp:Label>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridBoundColumn DataField="ShipName"
                        FilterControlAltText="Filter ShipName column" HeaderText="ShipName"
                        SortExpression="ShipName" UniqueName="ShipName">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="ShipCountry"
                        FilterControlAltText="Filter ShipCountry column" HeaderText="ShipCountry"
                        SortExpression="ShipCountry" UniqueName="ShipCountry">
                    </telerik:GridBoundColumn>
                </Columns>
                <EditFormSettings>
                    <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                    </EditColumn>
                </EditFormSettings>
            </MasterTableView>
            <FilterMenu EnableImageSprites="False">
            </FilterMenu>
        </telerik:RadGrid>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            SelectCommand="SELECT [OrderID], [OrderDate], [Freight], [ShipName], [ShipCountry] FROM [Orders]"></asp:SqlDataSource>
    </form>
</body>
</html>


Please help me to solve this issue.



Best regards.



Vasssek

0
Shinu
Top achievements
Rank 2
answered on 09 Dec 2013, 01:18 PM
Hi Vasssek,

I guess you want to apply Default Filter on Initial Load, hence you are setting EnableLinqExpressions="false". With EnableLinqExpressions="false",your required scenario cannot be obtained. You can set EnableLinqExpressions="true" and try the FilterExpression for default filter as mentioned here . Coding this way you can filter Integer GridBoundColumn as string. Below is a sample I have tried:

C#:
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    //When EnableLinqExpressions="true", the FilterExpression is as follows
    if (!Page.IsPostBack)
    {
        RadGrid1.MasterTableView.FilterExpression = @"it[""ShipCountry""].ToString().Contains(""Germany"")";
        GridColumn column = RadGrid1.MasterTableView.GetColumnSafe("ShipCountry");
        column.CurrentFilterFunction = GridKnownFunction.Contains;
        column.CurrentFilterValue = "Germany";
        RadGrid1.MasterTableView.Rebind();
    }      
}

Thanks,
Shinu
0
Vasssek
Top achievements
Rank 1
answered on 11 Dec 2013, 10:03 AM

Hi Shinu,

you're right. I use it in RadGrid1_PreRender where I need to traverse all pages in the RadGrid until the newly inserted item is found. The problem is that I have approx. 100000 records and standard steps (Radgrid.rebind and select next page) are taken too much time.

Now, I do it this way:

string filterExpression = RadGrid1.MasterTableView.FilterExpression.Length > 0 ? RadGrid1.MasterTableView.FilterExpression : string.Empty;
            string sortExpression = RadGrid1.MasterTableView.SortExpressions.Count > 0 ? RadGrid1.MasterTableView.SortExpressions[0].ToString() : string.Empty;
            int pageSize = RadGrid1.PageSize;
 
            int currentPageIndex = GetcurrentPageIndex(recordIDexpression, filterExpression, sortExpression, pageSize, (DataTable)RadGrid1.DataSource);
private int GetcurrentPageIndex(string recordIDexpression, string filterExpression, string sortExpression, int pageSize, DataTable dataTable)
   {        
       DataRow[] foundrows = null;
       DataRow[] foundrow = null;
       DataTable custom_dt;
       int currentPageIndex = -1;
 
       try
       {
           if (dataTable.Rows.Count > 0)
           {
               if (sortExpression.Length > 0)
               {
                   dataTable.DefaultView.Sort = sortExpression;
                   dataTable = dataTable.DefaultView.ToTable();
               }
 
               if (filterExpression.Length > 0)
               {
                   foundrows = dataTable.Select(filterExpression);
                   if (foundrows.Length > 0)
                   {
                       custom_dt = foundrows.CopyToDataTable();
                       foundrow = custom_dt.Select(recordIDexpression);
 
                       currentPageIndex = custom_dt.Rows.IndexOf(foundrow[0]) / pageSize;
                   }
               }
               else
               {
                   foundrow = dataTable.Select(recordIDexpression);
                   currentPageIndex = dataTable.Rows.IndexOf(foundrow[0]) / pageSize;
               }
           }
       }
       catch (Exception ex)
       {
       }
 
       return currentPageIndex;
   }



As you can see function GetcurrentPageIndex uses datatable.select with SQL filter expression (EnableLinqExpressions="false").


If I want to use numeric column filtering as string I must use EnableLinqExpressions="true" and rewrite datatable select and sort via linq language or do you have any idea ?


Best regards


Vasssek


0
Eyup
Telerik team
answered on 16 Dec 2013, 09:42 AM
Hi Vasssek,

To be able to filter numeric field as a string one, you need to keep EnableLinqExpressions set as True.  In case you want to navigate to the newly inserted item, you can use the approach demonstrated in the attached sample once you have the item's unique ID.

Hope this helps.

Regards,
Eyup
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Vasssek
Top achievements
Rank 1
answered on 19 Dec 2013, 09:57 AM
Hello Eyup,

your solution is nice, but can be used just in case when there isn't set any sortExpression or filterExpression in Radgrid. It seems that your function GetRecordPosition didn't accept this possibility...

Please, can you provide linq example even with sort and filter expression ?

P.S. It would be perfect, if something like this exist even for Radgrid:
http://decisivedata.net/server-side-paging-sorting-and-filtering-using-the-kendo-ui-grid-and-entity-framework-code-first/

Best regards

Vasssek
0
Eyup
Telerik team
answered on 24 Dec 2013, 09:15 AM
Hello Vasssek,

Yes, this is correct because the GetRecordPosition method returns the original record index from the datasource, but in the grid it has a different position after the Sort or Filter expression applied.

In that case, I suggest you to use the double Rebind() approach demonstrated in this sample:
http://www.telerik.com/community/code-library/aspnet-ajax/grid/get-selected-items-through-all-pages.aspx

Thank you for the suggestion. You can log it in our feedback portal so our developers take it under consideration:
http://feedback.telerik.com/Project/108

Regards,
Eyup
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Offir
Top achievements
Rank 1
answered on 31 Dec 2014, 01:39 PM
Hi, how do i apply this solution on Kendo Grid in Mvc?

i want to use "contains" on numeric field.
0
Alexander Popov
Telerik team
answered on 05 Jan 2015, 08:27 AM
Hello Offir,

The "contains" operator cannot be used on numeric fields. In case you need to perform such operations, I would suggest using a string field that holds the numbers, however that would cause issues with the sorting and would. That approach would also require a custom editor template which manually initializes a NumericTextBox widget, otherwise the fields would be edited in a regular textbox (since the field type is string).

Regards,
Alexander Popov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Offir
Top achievements
Rank 1
answered on 05 Jan 2015, 06:46 PM
I am not sure i undertsood that way, if u have an example...




In other issue, i have a kendo dropdown list that i can't bind an Enum to.
Every prop in the Enum have a description that i would like to display and a value. I use vb.net with mvc 3



0
Alexander Popov
Telerik team
answered on 08 Jan 2015, 01:22 PM
Hi Offir,

Here is an example:  
public class ProductViewModel
{
    public int ProductID
    { get; set; }
  
    public string ProductName
    { get; set; }
}

Now, if we have a Grid showing the IDs and the Product names, you can use the Contains filter on the ProductName but not on the ProductID. If, for example, the ProductID is of type string, then you would be able to use the Contains operator.

The issue with the DropDownList however is not related to this topic and I would ask you to open a new support ticket for it.

Regards,
Alexander Popov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Offir
Top achievements
Rank 1
answered on 08 Jan 2015, 02:03 PM
In my case i have a grid with a dateTime column:

columns.Bound(Function(p) p.PriorityEndDateTime).ClientTemplate("#=kendo.toString(PriorityEndDateTime,'yyyy/MM/dd')#").
                   HeaderTemplate("End Date").
                   HeaderHtmlAttributes(New With {.class = "col-xs-2"})

i understand it is not possible to filter on numeric columns such as dates so i thought to create a hidden column that will hold the date values as strings, the only problem is that i don't know how to convert those columns into strings.
please show me how.
best regards



Tags
Grid
Asked by
Mahdi
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Mahdi
Top achievements
Rank 1
Eyup
Telerik team
Vasssek
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Offir
Top achievements
Rank 1
Alexander Popov
Telerik team
Share this question
or