Problem while applying Filter programatically

3 posts, 0 answers
  1. Bibek
    Bibek avatar
    7 posts
    Member since:
    Dec 2008

    Posted 04 Oct 2010 Link to this post


    Hi,

    I want to apply the filter to radgridview programatically. This is the piece of code I am using :

    FilterExpression filter = null;
                    object currentCellValue = null;
                    if (grdSearch.CurrentCell != null) currentCellValue = grdSearch.CurrentCell.Value;
                    if (grdSearch.Columns[grdSearch.CurrentColumn.HeaderText].Filter != null)
                    {
                        filter = grdSearch.Columns[grdSearch.CurrentColumn.HeaderText].Filter;
                    }
                    else
                    {
                        filter = new FilterExpression(grdSearch.CurrentColumn.HeaderText);
                    }
                    if (grdSearch.CurrentCell.Value != null)
                    {
                        filter.Predicates.Add(FilterExpression.BinaryOperation.AND,
                                      GridKnownFunction.NotEqualTo,
                                      string.Format("@FilterEditor{0}",filter.Predicates.Count + 1));
                        filter.Parameters.Add(string.Format("@FilterEditor{0}",filter.Predicates.Count), currentCellValue);
                    }
                    else
                    {
                        filter.Predicates.Add(FilterExpression.BinaryOperation.AND, GridKnownFunction.NotIsNull, null);
                    }
                    
                    this.grdSearch.Columns[grdSearch.CurrentColumn.HeaderText].Filter = filter;

    This code is executed when user clicks the contextmenu opened after right clicking one of the gridcell.

    This piece of code works properly if the cell value has no special character. For example, it works if cell value is 'Microsoft Word' but
    if the cell value is 'MS Visio 3/4 Document/Drawing/Shapes/Te', it is being stored as 'MS Visio 3[/]4 Document[/]Drawing[/]Shapes[/]Te' by the Filter object and thus Filter is applied but is not working.

    Please check the attached image file to see how Filter is storing the parameters with special characters.

    regards,
    Bibek Dawadi

  2. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 04 Oct 2010 Link to this post

    Hello Bibek,

    Can you please try the attached application and please tell me what is wrong with the current filter.

    using System;
    using System.ComponentModel;
    using System.Windows.Forms;
    using Telerik.WinControls.Data;
    using Telerik.WinControls.UI;
     
    namespace TestGridFilter
    {
        public partial class Form1 : Form
        {
            private RadGridView radGridView1;
            public Form1()
            {
                InitializeComponent();
                radGridView1 = new RadGridView();
            }
     
            private void Form1_Load(object sender, EventArgs e)
            {
                radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
                radGridView1.EnableFiltering = true;
                this.Controls.Add(radGridView1);
                radGridView1.Dock = DockStyle.Fill;
     
                var radButton = new RadButton();
                radButton.Text = "Apply Filter";
                radButton.Click += new EventHandler(radButton_Click);
                radButton.Dock = DockStyle.Bottom;
                this.Controls.Add(radButton);
     
                radGridView1.DataSource = new TestsCollection(10);
            }
     
            void radButton_Click(object sender, EventArgs e)
            {
                Filter();
            }
     
            public void Filter()
            {
                FilterExpression filter = null;
                object currentCellValue = null;
                if (radGridView1.CurrentCell != null)
                    currentCellValue = radGridView1.CurrentCell.Value;
                if (radGridView1.Columns[radGridView1.CurrentColumn.HeaderText].Filter != null)
                {
                    filter = radGridView1.Columns[radGridView1.CurrentColumn.HeaderText].Filter;
                }
                else
                {
                    filter = new FilterExpression(radGridView1.CurrentColumn.HeaderText);
                }
     
                if (radGridView1.CurrentCell.Value != null)
                {
                    filter.Predicates.Add(FilterExpression.BinaryOperation.AND,
                                  GridKnownFunction.NotEqualTo,
                                  string.Format("@FilterEditor{0}", filter.Predicates.Count + 1));
                    filter.Parameters.Add(string.Format("@FilterEditor{0}", filter.Predicates.Count), currentCellValue);
                }
                else
                {
                    filter.Predicates.Add(FilterExpression.BinaryOperation.AND, GridKnownFunction.NotIsNull, null);
                }
     
                this.radGridView1.Columns[radGridView1.CurrentColumn.HeaderText].Filter = filter;
            }
        }
     
        public class Test
        {
            public int Id
            {
                get;
                set;
            }
     
            public string Text
            {
                get;
                set;
            }
     
            public Test(int id, string text)
            {
                this.Id = id;
                this.Text = text;
            }
        }
     
        public class TestsCollection : BindingList<Test>
        {
            public TestsCollection(int noItems)
            {
                for (int i = 0; i < noItems; i++)
                {
                    this.Add(new Test(i, i % 2 == 0 ? @"MS Visio 3/4 Document/Drawing/Shapes/Te" + i : "text" + i));
                }
            }
        }
    }

    A bit offtopic, if you are not using the latest version of Telerik controls you should consider updating to the latest version, because the FilterExpression property is obsolete and will be removed soon, or if you are using the latest version you should take a look at these following articles (Setting Filters Programmatically (simple descriptors) - http://www.telerik.com/help/winforms/grid_setting-filters-programmatically.html, Setting Filters Programmatically (composite descriptors) - http://www.telerik.com/help/winforms/setting-filters-programmatically2.html) in order to understant the new Filtering mechanism of the Grid, and to adapt your code acordingly.

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Alexander
    Admin
    Alexander avatar
    306 posts

    Posted 07 Oct 2010 Link to this post

    Hi Bibek,

    As Emanuel pointed, the Q2 2010 version of RadGridView control introduces the FilterDescriptors collection. A FilterDescriptor with FilterOperator = IsNotEqualTo and Value = "MS Visio 3/4 Document/Drawing/Shapes/Te" has the following expression:

    Info <> 'MS Visio 3/4 Document/Drawing/Shapes/Te'

    It will filter the data correctly in your scenario. If you have further questions, please do not hesitate to ask.

    Best regards,
    Alexander
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top