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

Problem while applying Filter programatically

2 Answers 147 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Bibek
Top achievements
Rank 1
Bibek asked on 04 Oct 2010, 03:37 PM

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 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 04 Oct 2010, 04:26 PM
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
0
Alexander
Telerik team
answered on 07 Oct 2010, 05:13 PM
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
Tags
GridView
Asked by
Bibek
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Alexander
Telerik team
Share this question
or