15 Answers, 1 is accepted
Yes, that is possible. The programmatic filtering entry point for RadGridView is the FilterDescription property -- you either provide your own, or configure the autogenerated CompositeFilterDescription. The latter contains a collection of FieldFilterDescription objects each defining the filter for the currently displayed columns. Here is how to define a filter by two columns:
public partial class Window1 : Window |
{ |
public Window1() |
{ |
InitializeComponent(); |
RadGridView1.ItemsSource = new Message[] { |
new Message { Sender = "tom@hanna-barbera.com", Subject = "Cats are cool", Size = 100 }, |
new Message { Sender = "jerry@hanna-barbera.com", Subject = "Mice are cool", Size = 100 }, |
new Message { Sender = "spike@hanna-barbera.com", Subject = "Dogs are cool", Size = 100 } |
}; |
} |
private void filterButton_Click(object sender, RoutedEventArgs e) |
{ |
var masterFilter = (CompositeFilterDescription) (this.RadGridView1.FilterDescription); |
var subjectFilter = (FieldFilterDescription) masterFilter.FilterDescriptions[0]; |
subjectFilter.Values.Add("Cats are cool"); |
subjectFilter.Values.Add("Mice are cool"); |
var senderFilter = (FieldFilterDescription) masterFilter.FilterDescriptions[1]; |
senderFilter.Values.Add("jerry@hanna-barbera.com"); |
} |
} |
Alternatively you can inherit from FilterDescription and create your own custom filtering. Our Custom Filtering example shows how to do that.
All the best,
Hristo Deshev
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Your code does not get the correct filter description for the subject filtering. Instead of using masterFilter.FilterDescriptions[1], you must use masterFilter.FilterDescriptions[0]. Changing that, returns 0 rows on my end.
All the best,
Hristo Deshev
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
I think I understand what you are trying to achieve here -- to reiterate: you need multi-column filtering that matches string in a case insensitive manner.
I've done that in a sample that:
- Uses a custom filter description: StringContainsFilterDescription. It uses reflection to get the value from the associated property and detect if the filter value is contained in the property value:
public override bool SatisfiesFilter(object dataItem) { //the line below needs better error checking and works for string properties only var value = (string) TypeDescriptor.GetProperties(dataItem)[this.PropertyName].GetValue(dataItem); if (!string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(this.FilterValue)) { return value.ToUpper().Contains(this.FilterValue.ToUpper()); } else { return string.IsNullOrEmpty(this.FilterValue); } } - We use two descriptions composed in a CompositeFilterDescription object.
- The two descriptions' filter values are obtained from two text boxes. That is why I have declared the filter descriptions in XAML and I am using a plain WPF binding to transfer the textbox Text property as a filter value:
<Window.Resources> <local:StringContainsFilterDescription x:Key="SenderFilter" PropertyName="Sender" /> <local:StringContainsFilterDescription x:Key="SubjectFilter" PropertyName="Subject" /> </Window.Resources>
Best wishes,
Hristo Deshev
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Please, find a simple example that demonstrates how to achieve your requirement. Let me know if it does not work for you.
All the best,
Valeri Hristov
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.
This is a known problem in RadGridView that has been fixed in our internal builds. The fix will be available in the official release of RadControls for WPF that is scheduled for the mid March.
Kind regards,
Valeri Hristov
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.
I tried to use this code
when I open the all project it works
but when I copy the code to my projet I get this error-
Error 1 'Telerik.Windows.Data.FieldFilterDescription' does not contain a definition for 'Values' and no extension method 'Values' accepting a first argument of type 'Telerik.Windows.Data.FieldFilterDescription' could be found (are you missing a using directive or an assembly reference?)
for this row
subjectFilter.Values.Add(
"Cats");
I didn't found any subjectFilter and Values.Add in the posted project - can you clarify?
Sincerely yours,
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.
var
masterFilter = (CompositeFilterDescription)(this.RadGridView2.FilterDescription);
var subjectFilter = (FieldFilterDescription)masterFilter.FilterDescriptions[0];
subjectFilter.Values.Add(
"Cats are cool");
subjectFilter.Values.Add(
"Mice are cool");
var senderFilter = (FieldFilterDescription)masterFilter.FilterDescriptions[1];
senderFilter.Values.Add(
"jerry@hanna-barbera.com");
but I get an error
'Telerik.Windows.Data.FieldFilterDescription' does not contain a definition for 'Values'
and no extension method 'Values' accepting a first argument of type 'Telerik.Windows.Data.FieldFilterDescription'
could be found (are you missing a using directive or an assembly reference?)
what I am looking for, is a simple code to filter by two columns.
Thanks.