Filtering NumericColumn with Contains

19 posts, 0 answers
  1. Mahdi
    Mahdi avatar
    27 posts
    Member since:
    Oct 2013

    Posted 08 Oct 2013 Link to this post

    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?
  2. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 08 Oct 2013 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Mahdi
    Mahdi avatar
    27 posts
    Member since:
    Oct 2013

    Posted 09 Oct 2013 Link to this post

    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.
  5. Eyup
    Admin
    Eyup avatar
    3015 posts

    Posted 11 Oct 2013 Link to this post

    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.
  6. Mahdi
    Mahdi avatar
    27 posts
    Member since:
    Oct 2013

    Posted 12 Oct 2013 Link to this post

    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.

     

  7. Eyup
    Admin
    Eyup avatar
    3015 posts

    Posted 17 Oct 2013 Link to this post

    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.
  8. Vasssek
    Vasssek avatar
    143 posts
    Member since:
    Aug 2010

    Posted 05 Dec 2013 Link to this post

    Hi Eyup,


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



    Thank you



    Vasssek



  9. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 05 Dec 2013 Link to this post

    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
  10. Vasssek
    Vasssek avatar
    143 posts
    Member since:
    Aug 2010

    Posted 05 Dec 2013 Link to this post

    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

  11. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 09 Dec 2013 Link to this post

    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
  12. Vasssek
    Vasssek avatar
    143 posts
    Member since:
    Aug 2010

    Posted 11 Dec 2013 Link to this post

    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


  13. Eyup
    Admin
    Eyup avatar
    3015 posts

    Posted 16 Dec 2013 Link to this post

    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.
  14. Vasssek
    Vasssek avatar
    143 posts
    Member since:
    Aug 2010

    Posted 19 Dec 2013 Link to this post

    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
  15. Eyup
    Admin
    Eyup avatar
    3015 posts

    Posted 24 Dec 2013 Link to this post

    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.
  16. Offir
    Offir avatar
    4 posts
    Member since:
    Dec 2014

    Posted 31 Dec 2014 in reply to Eyup Link to this post

    Hi, how do i apply this solution on Kendo Grid in Mvc?

    i want to use "contains" on numeric field.
  17. Alexander Popov
    Admin
    Alexander Popov avatar
    1416 posts

    Posted 05 Jan 2015 Link to this post

    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!
     
  18. Offir
    Offir avatar
    4 posts
    Member since:
    Dec 2014

    Posted 05 Jan 2015 in reply to Alexander Popov Link to this post

    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



  19. Alexander Popov
    Admin
    Alexander Popov avatar
    1416 posts

    Posted 08 Jan 2015 Link to this post

    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!
     
  20. Offir
    Offir avatar
    4 posts
    Member since:
    Dec 2014

    Posted 08 Jan 2015 in reply to Alexander Popov Link to this post

    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



Back to Top
UI for ASP.NET Ajax is Ready for VS 2017