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

Filtering by CONTAINS

6 Answers 309 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dmitry
Top achievements
Rank 1
Dmitry asked on 24 Apr 2009, 09:12 PM
I am loading one grid that will need to be filtered by user input of a value in a seperat textbox. The filter is a CONTAINS filter on one column of the grid.

Basically as the user types, the available values need to go down to better and better match the input. Do you guys have a good example of this?

Note: Values are plain text CONTAINS matching, so no special custom filters seem to be needed.

6 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 27 Apr 2009, 05:34 AM
Hi Dmitry,

Please check this demo for more info:
http://demos.telerik.com/wpf/?GridView/Search

All the best,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Dmitry
Top achievements
Rank 1
answered on 27 Apr 2009, 02:35 PM
Thank you for the pointer, but what code is inside of the custom class CustomFilterDescription?
0
Missing User
answered on 28 Apr 2009, 02:24 PM
Hello Dmitry,

You can find the CustomFilterDescription class here :
 <radcontrols installation folder>\Examples.CS\GridView\Search\CustomFilterDescription.cs

I'm also attaching the requested custom class for your convenience.

Please don't hesitate to contact us if you have other questions.

Kind regards,
Anastasia
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Kenny
Top achievements
Rank 1
answered on 18 May 2009, 07:20 PM
Hi there,
I'm working on a form with filtering by Containts as well, but using LINQ instead of using the ObjectDataProvider, but everytime the filter text got entered, the grid returned nothing (no records). I'm sure it's on my part since I'm pretty new to this WPF stuffs, so if you can help, that would be great. Here are the codes:

1. I have a "MusicData" class that acts as the gateway for me to get the data using LINQ.
public class MusicData 
    { 
        public int MusicID { getset; } 
        public string Title { getset; } 
        public string Title2 { getset; } 
        public string Artist { getset; } 
        public string Artist2 { getset; } 
        public string Key { getset; } 
        public string Path { getset; } 
        public string MP3 { getset; } 
 
        public static Notesnhac NotesnhacDataContext = new Notesnhac("Notesnhac.sdf"); 
 
 
        public static IQueryable<MusicData> GetMusicSheets() 
        { 
            var MusicSheets = NotesnhacDataContext.MusicSheets.Select(m => new MusicData 
            { 
                MusicID = m.ID, 
                Title = m.TITLE, 
                Artist = m.ARTIST, 
                Key = m.KEY 
            }); 
            return MusicSheets; 
        } 
    } 

2. In my XAML code behind file, Windows1.xaml.cs, I have a BindGrid() method that does the binding and call this method when this window got initilized, as well as the FilterValue_TextChanged method copied from the Example:

public Window1() 
        { 
            InitializeComponent(); 
 
            this.FilterTextBox.Focus(); 
            BindGrid(); 
        } 
 
        private void BindGrid() 
        { 
            var MusicSheets = MusicData.GetMusicSheets(); 
 
            radGridView1.ItemsSource = MusicSheets; 
        } 
 
        private void FilterValue_TextChanged(object sender, TextChangedEventArgs e) 
        { 
            if (this.FilterTextBox.Text.Length >= 1) 
            { 
                IEnumerable<string> propertyNames = from GridViewColumn column in this.radGridView1.Columns 
                                                    select column.UniqueName; 
                this.radGridView1.FilterDescription = new CustomFilterDescription(this.FilterTextBox.Text, propertyNames, false); 
            } 
            else 
            { 
                this.radGridView1.FilterDescription = null
            } 
        } 


3. I've added the TextBox and couple columns for the XAML file as supposed:

<telerik:RadGridView Name="radGridView1" telerik:Theming.Theme="Caramel" 
                                     ShowGroupPanel="False" IsFilteringAllowed="False" 
                                     Foreground="Black" Margin="0,30,0,0" AutoGenerateColumns="False"
 
                    <telerik:RadGridView.Columns> 
                        <telerik:GridViewDataColumn UniqueName="Title" HeaderText="Title" /> 
                        <telerik:GridViewDataColumn UniqueName="Artist" HeaderText="Artist" /> 
                        <telerik:GridViewDataColumn UniqueName="Key" HeaderText="Key" /> 
                    </telerik:RadGridView.Columns> 
                </telerik:RadGridView> 


4. I've modified the SatisfiesFilter method in the CustomFilterDescription class to match my MusicData object. I think this is where I'm not sure what to do and thus, it didn't returned anything when I did the filter:

public override bool SatisfiesFilter(object dataItem) 
        { 
            MusicData MusicSheets = (MusicData)dataItem; 
 
            if (string.IsNullOrEmpty(this.filterValue) || MusicSheets == null
            { 
                return false
            } 
 
            Type myDataType = MusicSheets.GetType(); 
 
            IEnumerable propertyValues = from property in myDataType.GetProperties() 
                                         where this.filterPropertyNames == null || this.filterPropertyNames.Contains(property.Name) 
                                         select property.GetValue(MusicSheets, null); 
 
            return (from object propertyValue in propertyValues 
                    where this.CheckPropertyValue(propertyValues) 
                    select propertyValue).Any(); 


Any help is appreciated.

Thanks,
Kenny.
0
Rossen Hristov
Telerik team
answered on 20 May 2009, 07:09 AM
Hello Kenny,

Here is the mistake:

return (from object propertyValue in propertyValues  
                    where this.CheckPropertyValue(propertyValues)  
                    select propertyValue).Any();  

You are passing the whole IEnumerable to the CheckPropertyValue method (which will always return false), where you actually should pass each concrete propertyValue. Remove the "s" I have highlighted in red and everything should work as expected. I hope this helps.

Please, do not hesitate to contact us if you have any further questions.

Greetings,
Ross
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Kenny
Top achievements
Rank 1
answered on 20 May 2009, 02:41 PM
Hi Ross,
Thank you so much for your help, that fixed the problem :)

Kenny.
Tags
GridView
Asked by
Dmitry
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Dmitry
Top achievements
Rank 1
Missing User
Kenny
Top achievements
Rank 1
Rossen Hristov
Telerik team
Share this question
or