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

Applying filtering in RadGridView programmatically

5 Answers 1176 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Konstantinos
Top achievements
Rank 1
Konstantinos asked on 21 Nov 2012, 09:17 PM

What I try to achieve is to apply a filter to my RadGridView whenever user checks / unchecks a checkbox (pretty straightforward, right?). My code works fine when I set the AutoGenerateColumns flag of my gridview to True, but does nothing when I switch the flag to False (the IsActive flag of my ColumnFilterDescriptor never changes to True). I need to apply a specific template to my columns so the AutoGenerateColumns flag has to be False. Both of the columns I try to filter have the IsFilterable flag to True and are binded to a string (and not a custom defined class). Here is my RadGridView:

<Merging:RadGridViewWithSelectedItemsEditable
      x:Name="SelectedUserAttributesGridView"
      Grid.Row="0"
           ItemsSource="{Binding MergeDetailsViewModel}" 
      AutoGenerateColumns="False" 
           VerticalAlignment="Top" 
           CanUserDeleteRows="False"
           CanUserFreezeColumns="False"
           SelectionMode="Extended"
      ScrollMode="Deferred"                          
      CanUserInsertRows="False" 
      CanUserReorderColumns="True" 
      CanUserResizeColumns="True" 
      CanUserSelect="False" 
      CanUserSortColumns="False" 
      ShowGroupPanel="False" 
      FrozenColumnsSplitterVisibility="Hidden" 
      GridLinesVisibility="None"                             
      RowIndicatorVisibility="Collapsed"                    
      IsEnabled="{Binding MergeDetailsEnabled}">
 
                   <Merging:RadGridViewWithSelectedItemsEditable.Columns>
                       <telerik:GridViewColumn Header="Well">
                           <telerik:GridViewColumn.CellTemplate>
                               <DataTemplate>
                                   <TextBlock VerticalAlignment="Center" Margin="3,0,3,0" Text="{Binding WellName}"  />
                               </DataTemplate>
                           </telerik:GridViewColumn.CellTemplate>
                       </telerik:GridViewColumn>
                       <telerik:GridViewColumn Header="UWI">
                           <telerik:GridViewColumn.CellTemplate>
                               <DataTemplate>
                                   <TextBlock VerticalAlignment="Center" Margin="3,0,3,0" Text="{Binding UWI}"  />
                               </DataTemplate>
                           </telerik:GridViewColumn.CellTemplate>
                       </telerik:GridViewColumn>
                       <telerik:GridViewColumn Header="{Binding SelectedAttributes[0].Name}" IsFilterable="True">
                           <telerik:GridViewColumn.CellTemplate>
                               <DataTemplate>
                                   <TextBlock VerticalAlignment="Center" Margin="3,0,3,0" Text="{Binding FirstSelectedAttributeValue}"  />
                               </DataTemplate>
                           </telerik:GridViewColumn.CellTemplate>                            
                       </telerik:GridViewColumn>
                       <telerik:GridViewColumn Header="{Binding SelectedAttributes[1].Name}" IsFilterable="True">
                           <telerik:GridViewColumn.CellTemplate>
                               <DataTemplate>
                                   <TextBlock VerticalAlignment="Center" Margin="3,0,3,0" Text="{Binding SecondSelectedAttributeValue}"  />
                               </DataTemplate>
                           </telerik:GridViewColumn.CellTemplate>
                       </telerik:GridViewColumn>
                       <telerik:GridViewColumn Header="Preview">
                           <telerik:GridViewColumn.CellTemplate>
                               <DataTemplate>
                                   <TextBlock VerticalAlignment="Center" Margin="3,0,3,0" Text="{Binding Preview}"  />
                               </DataTemplate>
                           </telerik:GridViewColumn.CellTemplate>
                       </telerik:GridViewColumn>
                   </Merging:RadGridViewWithSelectedItemsEditable.Columns>                   
                     
               </Merging:RadGridViewWithSelectedItemsEditable>

where FirstSelectedAttributeValue and SecondSelectedAttributeValue are both string properties of my class Well, and MergeDetailsViewModel an ObservableCollection<Well>.

Here is the code of the event handler for the checkbox that applies the filter:

private void CheckBoxClicked(object sender, RoutedEventArgs e)
{
    if (ShowHideNullValuesButton.IsChecked != null && (bool)ShowHideNullValuesButton.IsChecked)
    {
        ApplyNonNullFilter(2);
        ApplyNonNullFilter(3);             
    }
    else
        SelectedUserAttributesGridView.FilterDescriptors.Clear();
}
private void ApplyNonNullFilter(int columnIndex)
{
    Telerik.Windows.Controls.GridViewColumn attributeValueColumn =
  SelectedUserAttributesGridView.Columns[columnIndex];
    IColumnFilterDescriptor attributeValueColumnFilter =
  attributeValueColumn.ColumnFilterDescriptor;
    attributeValueColumnFilter.SuspendNotifications();
    attributeValueColumnFilter.FieldFilter.Filter1.Operator = FilterOperator.IsNotEqualTo;
    attributeValueColumnFilter.FieldFilter.Filter1.Value = "NaN";
    attributeValueColumnFilter.FieldFilter.Filter1.IsCaseSensitive = true;
    attributeValueColumnFilter.ResumeNotifications();
}

What am I missing? Why filtering fails? Btw, I don't get any errors or exceptions when building and running the code. Thanks.

5 Answers, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 26 Nov 2012, 01:57 PM
Hi Konstantinos,

 
I have tested your scenario using the code snippet you provided and I was not be able to reproduce any issue while applying programmatic filtering with GridView's property - AutoGenerateColumns="False" . I have attached sample project. In order to investigate it further, may I ask you to modify it in order to reproduce the problem, or send a sample project of your own?

Thank you in advance! 

All the best,
Yoan
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Rossen Hristov
Telerik team
answered on 26 Nov 2012, 02:47 PM
Hello,

Your manually defined columns are not GridViewDataColumns. We cannot filter a column which is not a data column since we don't know the property of the business object that it displays. When we autogenerate columns, we generate GridViewDataColumns which have a DataMemberBinding and that is how we know what property to filter on.

On the other hand your columns are not data columns, so we they cannot be part of any data operations. So you can either use GridViewDataColumns if you want to have filtering or you can try setting the FilterMemberPath of your columns to the property it is supposed to be filtering.

I hope this helps.

Kind regards,
Rossen Hristov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Konstantinos
Top achievements
Rank 1
answered on 26 Nov 2012, 04:43 PM
Hi Rossen and Yoan,

Thank you for your replies. Switching to GridViewDataColumns and using the DataMemberBinding fixed the issue. You might want to include snippets of the sample project you provided me to the documentation of the RadGridView control as it wasn't clear to me that I had to use the DataMemberBinding to apply my filters. Thanks again.

Regards,
Konstantinos
0
Dev
Top achievements
Rank 1
answered on 23 Oct 2014, 11:14 PM
Hi Telerik Team,

Thanks for the sample project attached with this post. I was referring this to implement similar kind of filtering functionality in RadGridView control.  Actually my requirement is to apply a default filter when RadGridView Control loaded and I am able to implement this using below code. But here I am using code behind file, how can we achieve same using viewModel?

public partial class MyView : UserControl
    {
        public MyView()
        {
            InitializeComponent();

            this.OutagesGridView.FilterDescriptors.SuspendNotifications();
            IColumnFilterDescriptor countryFilter = this.MyGridView.Columns["Status"].ColumnFilterDescriptor;
            countryFilter.SuspendNotifications();
            countryFilter.DistinctFilter.AddDistinctValue("Active");
            countryFilter.ResumeNotifications();
            this.MyGridView.FilterDescriptors.ResumeNotifications();
        }       
           }








0
Dimitrina
Telerik team
answered on 24 Oct 2014, 11:30 AM
Hello,

You could directly define filtering criteria for the source collection from your ViewModel. Does your bound collection expose FilterDescriptors collection? If not, in order to achieve your goal, you can wrap your collection  as a QueryableCollectionView. Then its FilterDescriptors will be directly synchronized with RadGridView.FilterDescriptors. 

I hope this helps.


Regards,
Dimitrina
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
GridView
Asked by
Konstantinos
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Rossen Hristov
Telerik team
Konstantinos
Top achievements
Rank 1
Dev
Top achievements
Rank 1
Dimitrina
Telerik team
Share this question
or