I have a radgridview and a Command to save changes to that view on a button next to it
<
Button
Content
=
"Save"
Margin
=
"0, 0, 8, 0"
Command
=
"{Binding Path=SaveAndNewCommand}"
>
<
Button.CommandParameter
>
<
Binding
ElementName
=
"Registrations"
/>
</
Button.CommandParameter
>
</
Button
>
When I save I want to clear any filters on this RadGridView
So in my Command I fire this in my UI thread when I am done
radGridView.FilterDescriptors.SuspendNotifications();
foreach (GridViewColumn column in radGridView.Columns)
{
column.ClearFilters();
}
radGridView.FilterDescriptors.ResumeNotifications();
I also tried
radGridView.FilterDescriptors.Clear();
but nothing seems to work, the lines aint filtered, but the filter button still shows the filtered state, and if I click on the filter icon, any settings I had before are still set.
9 Answers, 1 is accepted
I tried reproducing such behavior on my side, however I was not able to. When calling the ClearFilters() method of each column, the FilteringControl and the Filtering funnel are updated as expected.
Please find attached a sample application as a demonstration. Am I missing something?
Regards,
Stefan X1
Telerik
I use this custom filter, maybe thats why
001.
public
partial
class
CheckBoxFilterControl : IFilteringControl
002.
{
003.
public
ObservableCollection<CheckBoxCustomFilterType> CustomItems {
get
;
set
; }
004.
private
GridViewBoundColumnBase column;
005.
private
CompositeFilterDescriptor compositeFilter;
006.
007.
public
CheckBoxFilterControl()
008.
{
009.
InitializeComponent();
010.
CustomItems =
new
ObservableCollection<CheckBoxCustomFilterType>();
011.
BlankText =
"(Blank)"
;
012.
DataContext =
this
;
013.
014.
Loaded += Initialize;
015.
}
016.
017.
private
void
Initialize(
object
sender, RoutedEventArgs e)
018.
{
019.
Loaded -= Initialize;
020.
021.
PopUpParent.Opened += (s, a) => SetCachedValues();
022.
PopUpParent.Closed += (s, a) => CheckCachedValues();
023.
}
024.
025.
private
void
CheckCachedValues()
026.
{
027.
if
(cachedCustomItems ==
null
)
return
;
028.
029.
foreach
(var item
in
CustomItems)
030.
{
031.
var cachedItem = cachedCustomItems.Single(ci => ci.InternalValue == item.InternalValue && ci.Text == item.Text);
032.
if
(item.Checked != cachedItem.Checked)
033.
{
034.
item.Checked = cachedItem.Checked;
035.
}
036.
}
037.
038.
if
(SelectAllCheckBox.IsChecked.GetValueOrDefault() != cachedIsAllSelected)
039.
{
040.
SelectAllCheckBox.IsChecked = cachedIsAllSelected;
041.
}
042.
}
043.
044.
private
List<CheckBoxCustomFilterType> cachedCustomItems;
045.
private
bool
cachedIsAllSelected;
046.
private
void
SetCachedValues()
047.
{
048.
cachedIsAllSelected = SelectAllCheckBox.IsChecked.GetValueOrDefault();
049.
cachedCustomItems = CustomItems.Select(i=>
new
CheckBoxCustomFilterType(){Checked = i.Checked, InternalValue = i.InternalValue, Text = i.Text}).ToList();
050.
}
051.
052.
private
Popup internalPopUpParent =
null
;
053.
private
Popup PopUpParent
054.
{
055.
get
{
return
internalPopUpParent ?? (internalPopUpParent =
this
.ParentOfType<Popup>()); }
056.
}
057.
058.
private
void
OnFilter(
object
sender, RoutedEventArgs e)
059.
{
060.
if
(compositeFilter !=
null
)
061.
{
062.
column.DataControl.FilterDescriptors.Remove(compositeFilter);
063.
}
064.
065.
compositeFilter =
new
CompositeFilterDescriptor {LogicalOperator = FilterCompositionLogicalOperator.Or};
066.
var dataMember = column.DataMemberBinding.Path.Path;
067.
foreach
(var checkBoxCustomFilterType
in
CustomItems)
068.
{
069.
if
(checkBoxCustomFilterType.Checked)
070.
{
071.
var filter =
new
FilterDescriptor(dataMember, FilterOperator.IsEqualTo, checkBoxCustomFilterType.InternalValue);
072.
compositeFilter.FilterDescriptors.Add(filter);
073.
}
074.
}
075.
076.
if
(!column.DataControl.FilterDescriptors.Contains(compositeFilter))
077.
{
078.
column.DataControl.FilterDescriptors.Add(compositeFilter);
079.
}
080.
081.
SetCachedValues();
082.
083.
IsActive =
true
;
084.
085.
PopUpParent.IsOpen =
false
;
086.
}
087.
088.
private
void
OnClear(
object
sender, RoutedEventArgs e)
089.
{
090.
if
(compositeFilter !=
null
)
091.
{
092.
column.DataControl.FilterDescriptors.Remove(compositeFilter);
093.
}
094.
095.
CustomItems.ForEach(x => x.Checked =
false
);
096.
097.
SetCachedValues();
098.
099.
IsActive =
false
;
100.
101.
PopUpParent.IsOpen =
false
;
102.
}
103.
104.
public
void
Prepare(GridViewColumn c)
105.
{
106.
column = c
as
GridViewBoundColumnBase;
107.
108.
if
(column ==
null
)
109.
{
110.
return
;
111.
}
112.
113.
var distinctValues = ((RadGridView) column.Parent).GetDistinctValues(column,
false
);
114.
115.
foreach
(var distinctValue
in
distinctValues)
116.
{
117.
if
(CustomItems.Any(x => x.Text == DistinctValueToString(distinctValue)))
118.
{
119.
continue
;
120.
}
121.
122.
CustomItems.Add(
new
CheckBoxCustomFilterType
123.
{
124.
Checked =
false
,
125.
Text = DistinctValueToString(distinctValue),
126.
InternalValue = distinctValue
127.
});
128.
}
129.
if
(column !=
null
)
130.
{
131.
RegisterItemsSourceChanged(column.DataControl,
true
);
132.
}
133.
}
134.
135.
private
void
RegisterItemsSourceChanged(GridViewDataControl dataControl,
bool
register)
136.
{
137.
var property = DependencyPropertyDescriptor.FromProperty(DataControl.ItemsSourceProperty,
typeof
(GridViewDataControl));
138.
if
(property !=
null
)
139.
{
140.
if
(register)
141.
{
142.
property.AddValueChanged(dataControl, RemoveFilterWhenItemsSourceIsChanged);
143.
}
144.
else
145.
{
146.
property.RemoveValueChanged(dataControl, RemoveFilterWhenItemsSourceIsChanged);
147.
}
148.
}
149.
}
150.
151.
private
void
RemoveFilterWhenItemsSourceIsChanged(
object
sender, EventArgs e)
152.
{
153.
if
(compositeFilter !=
null
)
154.
{
155.
column.DataControl.FilterDescriptors.Remove(compositeFilter);
156.
}
157.
158.
RegisterItemsSourceChanged(column.DataControl,
false
);
159.
}
160.
161.
private
string
DistinctValueToString(
object
distinctValue)
162.
{
163.
return
distinctValue ==
null
? BlankText : distinctValue.ToString();
164.
}
165.
166.
public
bool
IsActive
167.
{
168.
get
{
return
(
bool
)GetValue(IsActiveProperty); }
169.
set
{ SetValue(IsActiveProperty, value); }
170.
}
171.
172.
public
string
BlankText {
get
;
set
; }
173.
174.
public
static
readonly
DependencyProperty IsActiveProperty =
175.
DependencyProperty.Register(
176.
"IsActive"
,
177.
typeof
(
bool
),
178.
typeof
(CheckBoxFilterControl),
179.
new
PropertyMetadata(
false
));
180.
181.
private
void
SelectAll(
object
sender, RoutedEventArgs e)
182.
{
183.
var checkbox = (sender
as
CheckBox);
184.
if
(checkbox ==
null
|| checkbox.IsChecked ==
null
)
185.
{
186.
return
;
187.
}
188.
189.
foreach
(var checkBoxCustomFilterType
in
CustomItems)
190.
{
191.
checkBoxCustomFilterType.Checked = checkbox.IsChecked.Value;
192.
}
193.
}
194.
}
I miss an edit feature...
When I run the
1.
radGridView.FilterDescriptors.SuspendNotifications();
2.
3.
foreach
(var col
in
radGridView.Columns)
4.
{
5.
col.ClearFilters();
6.
}
7.
8.
radGridView.FilterDescriptors.ResumeNotifications();
Then the OnClear in my custom filter is NOT run, if I click my button that runs that code, then it works fine, but I cant get to run that code from "outside" the filter (Like where I am trying to do it from)
As the scenario you describe seems pretty specific and based on the code provided it is hard for us to identify the root of the issue, would it be possible to provide us with a sample project that reflects your setup so we can debug it and investigate further? The following blog post should be of help - Isolating a Problem in a Sample Project. If more convenient for you, simply modify the project sent by my colleague.
Regards,
Stefan Nenchev
Telerik
Added my custom filter to your sample project,
Add filter to establised year, click button 1, nothing happens
Cant use your upload feature with my 38kb file (No image files) http://www.filedropper.com/cleargridviewfilters
You can expose a clear method in your custom filter and call it in case the filtering control of the column is of the custom type. The logic in the method should be the same as the one in the "OnClear()" method:
public
void
ClearFilter()
{
if
(compositeFilter !=
null
)
{
column.DataControl.FilterDescriptors.Remove(compositeFilter);
}
CustomItems.ForEach(x => x.Checked =
false
);
SetCachedValues();
IsActive =
false
;
PopUpParent.IsOpen =
false
;
}
Eventually, you can perform the filtering as follows:
private
void
Button1_Click(
object
sender, RoutedEventArgs e)
{
this
.clubsGrid.FilterDescriptors.SuspendNotifications();
foreach
(var col
in
this
.clubsGrid.Columns)
{
if
(col.FilteringControl !=
null
&& col.FilteringControl.GetType() ==
typeof
(CheckBoxFilterControl))
{
(col.FilteringControl
as
CheckBoxFilterControl).ClearFilter();
}
col.ClearFilters();
}
this
.clubsGrid.FilterDescriptors.ResumeNotifications();
}
Regards,
Stefan Nenchev
Telerik