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

MultiSelectComboBoxColumn Filter

4 Answers 126 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Martin
Top achievements
Rank 1
Martin asked on 09 Apr 2014, 05:24 PM
Hello,

i use the "Mutiselect drop down list column" from http://www.telerik.com/support/kb/winforms/gridview/details/mutiselect-drop-down-list-column-in-radgridview
I'm stuck in the implementation of a filter function (Filter must work in combine with other Columns). I found part of codes for custom filter functions and tried to combine them

The basic filter function works already:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using Telerik.WinControls;
using Telerik.WinControls.Data;
using System.Diagnostics;
 
namespace _547099
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
 
            DataTable t = new DataTable();
            t.Columns.Add("ID", typeof(int));
            t.Columns.Add("Name", typeof(string));
            t.Rows.Add(1, "one");
            t.Rows.Add(2, "two");
            t.Rows.Add(3, "three");
            t.Rows.Add(4, "four");
            t.Rows.Add(5, "five");
            t.Rows.Add(6, "six");
            t.Rows.Add(7, "seven");
            t.Rows.Add(8, "eight");
            t.Rows.Add(9, "nine");
            t.Rows.Add(10, "ten");
 
            CustomColumn col = new CustomColumn("MutiSelect column");
            col.Name = "Name";
            col.DataSource = t;
            col.DisplayMember = "Name";
            col.ValueMember = "ID";
            FilterDescriptor descriptor = new FilterDescriptor("Name", FilterOperator.Contains, 0);
            //col.AllowFiltering = false;
             
             
            radGridView1.Columns.Add(col);
 
            GridViewTextBoxColumn col2 = new GridViewTextBoxColumn();
            col2.Name = "Test";
            radGridView1.Columns.Add(col2);
 
            radGridView1.Rows.Add(new object[] { new int[] { 9, 6, 10 }, "ab"});
            radGridView1.Rows.Add(new object[] { new int[] { 5, 1, 3 }, "abcd"});
            radGridView1.Rows.Add(new object[] { new int[] { 8, 7, 1 }, "ef"});
            radGridView1.Rows.Add(new object[] { new int[] { 4, 2, 1 }, "fgh" });
 
            this.radGridView1.EnableFiltering = true;
            this.radGridView1.ShowHeaderCellButtons = true;
            this.radGridView1.EnableCustomFiltering = true;           
            this.radGridView1.CustomFiltering += new GridViewCustomFilteringEventHandler(radGridView1_CustomFiltering);
 
            this.radGridView1.CellEndEdit += new GridViewCellEventHandler(radGridView1_CellEndEdit);
            this.radGridView1.CellValidating += new CellValidatingEventHandler(radGridView1_CellValidating);
            this.radGridView1.FilterExpressionChanged += radGridView1_FilterExpressionChanged;
            
        }
 
        void radGridView1_FilterExpressionChanged(object sender, FilterExpressionChangedEventArgs e)
        {
            Debug.WriteLine("Filter geändert: " + e.FilterExpression);           
        }
 
        void radGridView1_CellValidating(object sender, CellValidatingEventArgs e)
        {
            if (radGridView1.ActiveEditor == null ||!( e.Row is GridViewFilteringRowInfo)) return;
 
            if (e.Column.GetDefaultEditorType() == typeof(CustomDropDownListEditor))
            {
                filterValues.Clear();
                foreach (CustomListDataItem item in ((CustomEditorElement)(((CustomDropDownListEditor)(this.radGridView1.ActiveEditor)).EditorElement)).Items)
                {
                    if (item.Selected)
                    {
                        filterValues.Add((int)item.Value);
                    }
                }
            }
        }
 
        List<int> filterValues = new List<int>();
        void radGridView1_CellEndEdit(object sender, GridViewCellEventArgs e)
        {
            Debug.WriteLine("Refresh");
            this.radGridView1.MasterTemplate.Refresh();
        }       
         
        private void radGridView1_CustomFiltering(object sender, GridViewCustomFilteringEventArgs e)
        {
            if (filterValues.Count == 0)
            {               
                Debug.WriteLine("FilterVal 0");
                int fdi = radGridView1.FilterDescriptors.IndexOf("Name");
                if (fdi != -1)
                {
                    radGridView1.FilterDescriptors.RemoveAt(fdi);
                }
            }
 
            if (radGridView1.FilterDescriptors.Count == 0)
            {
                return;
            }
 
            if (filterValues.Count > 0)
            {               
                bool shouldVisible = false;
                foreach (int cellValue in ((int[])e.Row.Cells["Name"].Value))
                {
                    foreach (int j in filterValues)
                    {
                        if (cellValue == j)
                        {
                            shouldVisible = true;
                            break;
                        }
                    }
                }
                e.Visible = shouldVisible;
            }
 
            if (e.Visible) e.Handled = false;
        }
    }
}

So i can filter in custom column "Name" (MultiSelectDropdownColumn) and also in Column2 "Test" (TextboxColumn). good so far

After i filtered something in Column "Test" followed by clear this filter, i try to filter in custom column "Name" again but this time the filter don't work (all Rows will hidden or only one matching row will shown).

Does anyone have any idea where the fault lies?

4 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 10 Apr 2014, 01:05 PM
Hi Martin,

Thank you for writing.

I tried to run your solution, but I was unable to get it working. Instead, I have created a simpler solution, where the column will not be a combo column, instead, it will use a text box column holding a string with the multiple selected values. Once an editor is requested, we will provide a drop down editor and we will select the items needed. This approach seems to work correctly, including filtering by multiple columns. Please give it a try and let me know how does it work for you.

Regards,
Stefan
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Martin
Top achievements
Rank 1
answered on 10 Apr 2014, 01:28 PM
Hello Stefan,

many thanks for your effort.

That would work for me too, with a small extension:
If i filter with only one value, the default "contains" filter works well, but for my project i need the possibility to filter with a few values by "or condition":
For example: i checked in filter "one" and "two" -> rows must been shown with containing "one" OR "two" value
So only one of the values ​​must be present in a row.

Regards,
Martin
0
Accepted
Stefan
Telerik team
answered on 11 Apr 2014, 12:23 PM
Hello,

To do that, you can cancel the default filtering when the grid attempts to filter by the Name column and use a CompositeFilterDescriptor to handle it on your own. Attached you can find a sample of this.

If this does not work for you, you can try the custom filtering abilities of the control: http://www.telerik.com/help/winforms/gridview-filtering-custom-filtering.html.

I hope I was able to help.

Regards,
Stefan
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Martin
Top achievements
Rank 1
answered on 12 Apr 2014, 10:52 AM
Many thanks it works great.

Regards
Martin
Tags
GridView
Asked by
Martin
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Martin
Top achievements
Rank 1
Share this question
or