I have filter enabled for all of my grid view columns, and would like to change default filter option from "Is Equal to" to "Contains" How can I achinve this?
Thanks
Amit
18 Answers, 1 is accepted
You may customize the filtering control by creating one of your own and change the initial selected value inside the two combo boxes in the filter. It may be like follows:
public class CustomizedFilteringControl : FilteringControl
{
public override void Prepare(GridViewBoundColumnBase column)
{
base.Prepare(column);
var availableOperators = new List<
FilterOperator
>()
{
FilterOperator.Contains,
FilterOperator.IsEqualTo,
FilterOperator.IsNotEqualTo,
FilterOperator.StartsWith,
FilterOperator.EndsWith,
FilterOperator.DoesNotContain,
FilterOperator.IsContainedIn,
};
var cbo1 = this.ChildrenOfType<
RadComboBox
>()
.Where(b => b.Name == "PART_Filter1ComboBox")
.FirstOrDefault();
cbo1.SelectedValue = availableOperators.FirstOrDefault();
cbo1.ItemsSource = availableOperators;
var cbo2 = this.ChildrenOfType<
RadComboBox
>()
.Where(b => b.Name == "PART_Filter2ComboBox")
.FirstOrDefault();
cbo2.SelectedValue = availableOperators.FirstOrDefault();
cbo2.ItemsSource = availableOperators;
}
}
You may define the filtering control for the required column in xaml, in the code-behind or during the AutoGeneratingColumn event of the grid:
XAML:
<
telerik:GridViewDataColumn
DataMemberBinding
=
"{Binding Name}"
>
<
telerik:GridViewDataColumn.FilteringControl
>
<
my:CustomizedFilteringControl
/>
</
telerik:GridViewDataColumn.FilteringControl
>
</
telerik:GridViewDataColumn
>
C#:
var nameColumn = (GridViewBoundColumnBase)this.clubsGrid.Columns["Name"];
var customFilteringControl = new CustomizedFilteringControl();
nameColumn.FilteringControl = customFilteringControl ;
Greetings,
Maya
the Telerik team
The posted solution does not work properly. At first it seems ok but somehow but when you reopen the filter i have encountered the following problems:
a) the displayed selection has changed
b) the grid is apparently filtered by another selected that does neither matches the one displayed or originally set.
This is an important issue for us as we are using the GridView control in combination with the domain datasource and ria services. The filter operators "IsContainedIn" and IsNotContained in will result in an exception. Please help.
thanks in advance
The following approach has the same problems
public
override
void
Prepare(GridViewBoundColumnBase column)
{
base
.Prepare(column);
if
(column.DataType ==
typeof
(
string
))
// Set contains as default only for string data type of the gridview column
{
var vm =
this
.DataContext
as
FilteringViewModel;
if
(vm !=
null
)
{
if
(vm.AvailableActions.Contains(FilterOperator.IsNotContainedIn))
{
vm.AvailableActions.Remove(FilterOperator.IsNotContainedIn);
}
if
(vm.AvailableActions.Contains(FilterOperator.IsContainedIn))
{
vm.AvailableActions.Remove(FilterOperator.IsContainedIn);
}
}
You can remove the setting of the selected value of the combo box:
var cbo1 = this.ChildrenOfType<
RadComboBox
>()
.Where(b => b.Name == "PART_Filter1ComboBox")
.FirstOrDefault();
cbo1.ItemsSource = availableOperators;All the best,
Maya
the Telerik team
Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>
I almost thought that i have found the solution. Just temporarily store the value in a variable and reassign it at the end (makes kind of sense if there is a two-way binding between the viewmodel and the control):
public class DefaultFilter : FilteringControl
{
public override void Prepare(GridViewBoundColumnBase column)
{
base.Prepare(column);
if (column.DataType == typeof(string))
// Set contains as default only for string data type of the gridview column
{
var vm = this.DataContext as FilteringViewModel;
if (vm != null)
{
var filter1 = vm.Filter1.Operator; // save filter value in a variable as they are changed when bound to the combobox (probably a two-way binding)
var filter2 = vm.Filter2.Operator;
// approach recommended by telerik : http://www.telerik.com/community/forums/silverlight/gridview/gridview-columnfilter-option.aspx
var availableOperators = new List<
FilterOperator
>()
{
FilterOperator.IsEqualTo,
FilterOperator.IsNotEqualTo,
FilterOperator.StartsWith,
FilterOperator.EndsWith,
FilterOperator.Contains,
FilterOperator.DoesNotContain,
};
var comboBox1 = this.ChildrenOfType<
RadComboBox
>()
.Where(b => b.Name == "PART_Filter1ComboBox")
.FirstOrDefault();
comboBox1.SelectedValue = availableOperators.FirstOrDefault();
comboBox1.ItemsSource = availableOperators;
var comboBox2 = this.ChildrenOfType<
RadComboBox
>()
.Where(b => b.Name == "PART_Filter2ComboBox")
.FirstOrDefault();
comboBox2.SelectedValue = availableOperators.FirstOrDefault();
comboBox2.ItemsSource = availableOperators;
if (vm.Filter1.IsActive)
{
comboBox1.SelectedValue = filter1;
}
if (vm.Filter2.IsActive)
{
comboBox2.SelectedValue = filter2;
}
However, this doews not fully work!!
How to reproduce:
- 1. Set filter settings (Filter 1: Equeals to a certain value, "OR"-Setting, Filter2: Equeals to another value) -> this will filter correctly
- 2. Reopen filter-> filter will start to behave strangely: filtering in grid is going to be flawed. Close and reopen filter-> displayed values are wrong
My guess is, that i would have to have to reapply not only the operator values to the two comboboxes but also all other values.
How can i make the gridview filter correctly? To me, this seems to be a major issue. Filtering is a core functionality!
Thanks in advance
By the way: we are using Silverlight 4, Ria Services (which is why we had to remove the ContainIn-Stuff), and the latest release of telerik controls.
May you clarify why the suggested approach is not appropriate for you and you want to extend its functionality ? What is the behavior you want to achieve ?
Maya
the Telerik team
Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>
Hi
If you use the adapted default filter (see my pasted code for the Prepare(GridViewBoundColumnBase column) method, you just have to set filtersettings (for filter 1 and filter 2) and then close and reopen the filter a couple of times and you will see that filtering does not work any longer. What do I have to do to make this work? It is actually pretty easy to reproduce.
PS 1: We use ria services, silverlight 4 and the latest release of silverlight controls.
PS 2: Ive just seen that the Operators "IsContainedIn" and "IsNotContainedIn" now work also for if the grid is bound to a ria service method. This used to be different. This would allow us to go with the default filter, although i am not happy with it as the "contained in" operators are confusing to the user.
Unfortunately, I am not quite sure why you are extending the functionality of the Prepare method in such a manner. What is the behavior that you want to get ?
Indeed, the code provided by you will cause some undesired behavior, but in fact that is the expected one, since you are trying to force the setting of the SelectedValue of the RadComboBox-es. You need to leave that job to the filter itself. Thus once you filter and reopen the filtering control, the correct value will be displayed in the combo boxes.
Considering the two operators - "Is contained in" and "Is not contained in" - their presence depends on the type of the property bound to the corresponding column, not on whether you are calling a particular method. Why do you consider them confusing, what will be the behavior you expect when using them ?
Maya
the Telerik team
Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>
I dont really think that the two operators - "Is contained in" and "Is not contained in" - are that confusing, but my customer and the people who have been testing (the endusers) seem to have problem with it.
If there is a simple way of getting rid of them without any side effects, i would be glad. If this is not so easily possible, it is also ok.
Greetings
The way to go for the time being would be to create your own custom filtering control (just as illustrated in this forum thread). Still, I am sending you a sample project illustrating a working solution, implementing the suggested approach.
Maya
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
But for some reason I somethimes do not see the filter funnel turned on when I filtered my data. This is specially annoying when you close the filter control because then you don't know which column you should clear to get all data again.
I already checked the forum (e.g. http://www.telerik.com/community/forums/silverlight/gridview/force-the-funnel-icon-of-filtering-control-to-light-up.aspx) but could not yet find an answer how I can set the funnel manually which I don't think is a good solution but for the moment the only one I see.
Thanks for your help. Cheers,
christoph
I have already created my custom Filter using your approach described here in this post. Today we've decided to Update or Controls to the newest version (Q1 2012). Now I'm facing some problems, I saw at the documentation that you need to do some changes in this new version, but i couldn't manage to found a solution just reading there. Would you please provide some way to fix the Filtering in this new version? Here is my code:
public
class
CustomFilteringControl : FilteringControl
{
public
override
void
Prepare(GridViewColumn gridcolumn)
{
base
.Prepare(gridcolumn);
var vm =
this
.DataContext
as
FilteringViewModel;
GridViewBoundColumnBase column = gridcolumn
as
GridViewBoundColumnBase;
if
(vm !=
null
)
{
if
(!vm.Filter1.IsActive)
{
if
(column.DataType.ToString().Contains(
"System.Int"
))
{
vm.Filter1.Operator = FilterOperator.IsEqualTo;
}
else
{
vm.Filter1.Operator = FilterOperator.Contains;
}
}
if
(!vm.Filter2.IsActive)
{
if
(column.DataType.ToString().Contains(
"System.Int"
))
{
vm.Filter2.Operator = FilterOperator.IsEqualTo;
}
else
{
vm.Filter2.Operator = FilterOperator.Contains;
}
}
}
}
}
Thanks.
I have some very good news. There is no longer a need to inherit the stock FilteringControl simply to change the default operator. That is just too complex.
RadGridView has an event called FilterOperatorsLoading. In the event arguments you have the column for which the operators are being loaded. You can remove some of them if you want to. You can also specify the default operator to be selected when the end-user opens the filtering control. It's all there in the event arguments.
Let me know if there are problems.
Ross
the Telerik team
It would be better to change the order of the following lines
cbo1.SelectedValue = availableOperators.FirstOrDefault();
cbo1.ItemsSource = availableOperators;
to
cbo1.ItemsSource = availableOperators;
cbo1.SelectedValue = availableOperators.FirstOrDefault();
Same change for cbo2 too.
Reimund
the defaults are not honored if you hit the clear filters button.
Private
Sub
RadGridView_FilterOperatorsLoading(sender
As
System.
Object
, e
As
Telerik.Windows.Controls.GridView.FilterOperatorsLoadingEventArgs)
If
e.Column.UniqueName =
"Message"
Then
e.DefaultOperator1 = Telerik.Windows.Data.FilterOperator.Contains
e.DefaultOperator2 = Telerik.Windows.Data.FilterOperator.DoesNotContain
End
If
End
Sub
Can you please tell us the exact version that you are using?
Regards,Ross
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
Thanks
We fixed the issue and it will be working properly in the next Latest Internal Build.
Kind regards,Ross
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>