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

FilterDescriptor for NULL value on Nullable

3 Answers 146 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Reuben Russell
Top achievements
Rank 1
Reuben Russell asked on 26 May 2010, 06:15 PM
I was having trouble finding a solution to filtering on null via code.  I found the solution and wish to share that with the community as I could not find the solution here.

...  
private IDataFieldDescriptor DataMember  
...  
 
public BuildFilter()  
{  
    ...  
      ColumnFilterDescriptor filter = new ColumnFilterDescriptor(DataMember);  
 
      if (OnlyNulls)  
      {  
             filter.FieldFilter.FilterDescriptors.Add(  
                 new FilterDescriptor(DataMember.DataMemberBinding.Path.Path, FilterOperator.IsEqualTo, (DateTime?)null));  
      }    
 
    ...  
}  
 

I have cliped out non-relevent code.  The filter object that is created above will allow the correct filtering to happen, but using the following will NOT work.

(WONT WORK)
...  
filter.FieldFilter.Filter1.Operator = FilterOperator.IsEqualTo;  
filter.FieldFilter.Filter1.Value = (DateTime?)null;  
... 

3 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 27 May 2010, 07:48 AM
Hi Reuben Russell,

You can do this like this:

ColumnFilterDescriptor cfd = new ColumnFilterDescriptor((GridViewBoundColumnBase)this.clubsGrid.Columns[1]);
cfd.DistinctFilter.DistinctValues.Add(null);
this.clubsGrid.FilterDescriptors.Add(cfd);

When the value of a field filter is null we treat it as inactive and that is why your approach will not work. For a future version we might include something similar to DependencyProperty.UnsetValue in order to indicate that the filter value is unset, since null is a valid value in cases like yours.

Currently, the other way to overcome this limitation is to develop your very own custom filtering control and define the custom logic according to your needs. In other words, your custom control will treat filters with value equal to null as valid and active and the correct filtering would be performed.

I hope this helps.

Greetings,
Ross
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Steven
Top achievements
Rank 1
answered on 29 Jun 2011, 05:09 PM
Is there a way to treat the value as inactive when it is null with a FilterDescriptor ?

In your sample, you are using ColumnFilterDescriptor.
There is no FilterDescriptor.Distinct...

Regards,
S
0
Rossen Hristov
Telerik team
answered on 30 Jun 2011, 05:26 AM
Hello Steven,

I am not sure which sample are you talking about.

Anyway, there is a special static singleton value called FilterDescriptor.UnsetValue. If this value is assigned to the Value property of a FilterDescriptor it is treated as inactive and excluded from from the generation of the LINQ Where expression. 'null', on the other hand is a perfectly valid value just like '2', string.Empty and everything else possible.

I hope this helps.

Regards,
Ross
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Reuben Russell
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Steven
Top achievements
Rank 1
Share this question
or