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

[Solved] My Custom Filter Row only works properly when column is sorted

2 Answers 118 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Nicole
Top achievements
Rank 1
Nicole asked on 05 Feb 2011, 05:31 PM
Hi all,

I have implemented the Custom Filter Row example from the Telerik RadGridView demo page. I have used the new CustomFilterRow class you have provided in this forum : 
http://www.telerik.com/community/forums/silverlight/gridview/custom-filter-row-with-resizing-and-reordering.aspx

And as we are loading the data asynchronously, I have used some of the information described here:

http://www.telerik.com/community/forums/silverlight/gridview/fitting-the-customfilterrow-in-a-mvvm-application-and-other-changes.aspx

Further, I have implemented that the filter will be applied when you press Enter.

 Now, when I am activating a filter, the first time it works fine. The second time there is no data displayed in the Grid, even if there are valid entries. The third time it works fine again, and so on...

All these issues disappear when the filtered cell is sorted.
Do you have an idea what the problem could be?

Regards, Nicole


Here is my  CustomFilterRow.cs:

using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System.Windows.Data;
using System.ComponentModel;
  
namespace Astra.MistraBS.WebClient.UI.UserControls.Filter
{
    public class CustomFilterRow
    {
        private RadGridView radGridView;
        private FilteringRow filteringRow;
  
        public CustomFilterRow(RadGridView radGridView)
        {
              
            this.radGridView = radGridView;
            this.radGridView.IsFilteringAllowed = false;
            this.filteringRow = new FilteringRow();
  
            this.radGridView.DataLoaded += this.radGridView_DataLoaded;
  
            this.radGridView.RowLoaded += this.radGridView_RowLoaded;
            this.radGridView.ColumnDisplayIndexChanged += this.ReorderFilterCells;
            this.radGridView.Grouped += this.RecalculateIndentPresenterWidth; 
        }
  
        private void radGridView_DataLoaded(object sender, EventArgs e)
        {
              
            this.filteringRow.Loaded += this.FilteringRow_Loaded;
  
            this.radGridView.DataLoaded -= this.radGridView_DataLoaded;
        }
  
        public static bool GetIsEnabled(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsEnabledProperty);
        }
  
        public static void SetIsEnabled(DependencyObject obj, bool value)
        {
            obj.SetValue(IsEnabledProperty, value);
        }
  
        public static readonly DependencyProperty IsEnabledProperty =
            DependencyProperty.RegisterAttached("IsEnabled", typeof(bool),
                typeof(CustomFilterRow),
                new PropertyMetadata(new PropertyChangedCallback(OnIsEnabledPropertyChanged))
            );
  
        private static void OnIsEnabledPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            RadGridView radGridView = dependencyObject as RadGridView;
            if (radGridView != null)
            {
                if ((bool)e.NewValue)
                {
                    CustomFilterRow row = new CustomFilterRow(radGridView);
                }
            }
        }
  
        private void FilteringRow_Loaded(object sender, RoutedEventArgs e)
        {
            GridViewScrollViewer gridViewScrollViewer = this.radGridView.ChildrenOfType<GridViewScrollViewer>().FirstOrDefault();
            gridViewScrollViewer.ScrollChanged += this.GridViewScrollViewer_ScrollChanged;
  
            this.BindToColumns();
  
            this.PlaceCommonHeaders();
  
            this.filteringRow.CustomFilterRowGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
        }
  
        private void radGridView_RowLoaded(object sender, RowLoadedEventArgs e)
        {
            if (e.Row is GridViewHeaderRow)
            {
                Grid rootPanelGrid = (from c in this.radGridView.ChildrenOfType<Grid>()
                                      where c.Name == "PART_RootPanel"
                                      select c).FirstOrDefault();
  
                if (rootPanelGrid != null && this.filteringRow.Parent == null)
                {
                    rootPanelGrid.Loaded += this.rootPanelGrid_Loaded;
                }
  
                Border indicator = (from c in this.radGridView.ChildrenOfType<Border>()
                                    where c.Name == "PART_IndicatorPresenter"
                                    select c).FirstOrDefault();
  
                if (indicator != null)
                {
                    this.filteringRow.CustomFilterRowRoot.ColumnDefinitions[0].Width = GridLength.Auto;
                    Border indicatorFiller = new Border();
                    indicatorFiller.Name = "PART_IndicatorPresenterForFilterRow";
                    indicatorFiller.SetValue(Grid.ColumnProperty, 0);
                    this.filteringRow.CustomFilterRowRoot.Children.Add(indicatorFiller);
  
                    Binding widthBinding = new Binding("Width");
                    widthBinding.Source = indicator;
                    indicatorFiller.SetBinding(Border.WidthProperty, widthBinding);
  
                    Binding visibilityBinding = new Binding("Visibility");
                    visibilityBinding.Source = indicator;
                    indicatorFiller.SetBinding(Border.VisibilityProperty, visibilityBinding);
                }
            }
        }
  
        void rootPanelGrid_Loaded(object sender, RoutedEventArgs e)
        {
            Grid rootPanelGrid = sender as Grid;
            if (this.filteringRow.Parent == null)
            {
                rootPanelGrid.Children.Add(this.filteringRow);
                this.filteringRow.SetValue(Grid.RowProperty, 1);
            }
        }
  
        private void RecalculateIndentPresenterWidth(object sender, GridViewGroupedEventArgs e)
        {
            double presenterWidth = this.filteringRow.CustomFilterRowRoot.ColumnDefinitions[0].Width.Value;
            this.filteringRow.CustomFilterRowRoot.ColumnDefinitions[1].Width = new GridLength(this.radGridView.GroupDescriptors.Count * presenterWidth);
        }
  
        private void BindToColumns()
        {
            for (int i = 0; i < this.NumberOfColumnsInGridView(this.radGridView); i++)
            {
                ColumnDefinition columndefinition = new ColumnDefinition();
                columndefinition.Width = GridLength.Auto;
                this.filteringRow.CustomFilterRowGrid.ColumnDefinitions.Add(columndefinition);
            }
        }
  
        private void PlaceCommonHeaders()
        {
            for (int i = 0; i < this.NumberOfColumnsInGridView(this.radGridView); i++)
            {
                ColumnFilterCell filterCell = null;
                if (this.radGridView.Columns[i] is GridViewBoundColumnBase)
                {
                    filterCell = ColumnFilterCellFactory.CreateColumnHeader((GridViewBoundColumnBase)this.radGridView.Columns[i]);
                }
                else
                {
                    filterCell = new ColumnFilterCell();
                }
                filterCell.Column = this.radGridView.Columns[i];
                filterCell.PropertyChanged += this.FilterCell_PropertyChanged;
                filterCell.SetValue(Grid.ColumnProperty, i);
                this.filteringRow.CustomFilterRowGrid.Children.Add(filterCell);
                filterCell.Loaded += this.filterCell_Loaded;
            }
  
            ColumnFilterCell lastEmptyHeader = new ColumnFilterCell();
            lastEmptyHeader.SetValue(Grid.ColumnProperty, this.filteringRow.CustomFilterRowGrid.ColumnDefinitions.Count());
            this.filteringRow.CustomFilterRowGrid.Children.Add(lastEmptyHeader);
        }
  
        private static void BindWidthAndVisibilityOnFilterCell(ref ColumnFilterCell cell, GridViewColumn column)
        {
            Binding columnWidthBinding = new Binding("ActualWidth");
            columnWidthBinding.Source = column;
            Binding columnVisibilityBinding = new Binding("IsVisible");
            columnVisibilityBinding.Converter = new BooleanToVisibilityConverter();
            columnVisibilityBinding.Source = column;
            cell.SetBinding(ColumnFilterCell.VisibilityProperty, columnVisibilityBinding);
            cell.SetBinding(ColumnFilterCell.WidthProperty, columnWidthBinding);
        }
  
        void filterCell_Loaded(object sender, RoutedEventArgs e)
        {
            ColumnFilterCell filterCell = sender as ColumnFilterCell;
            if (filterCell.Column.MinWidth < filterCell.MinWidth)
            {
                filterCell.Column.MinWidth = filterCell.MinWidth;
            }
            BindWidthAndVisibilityOnFilterCell(ref filterCell, filterCell.Column);
        }
  
        private void ReorderFilterCells(object sender, GridViewColumnEventArgs e)
        {
            ColumnFilterCell firstCell = (from c in this.filteringRow.CustomFilterRowGrid.Children.OfType<ColumnFilterCell>()
                                          where c.Column == e.Column
                                          select c).FirstOrDefault();
  
            if (firstCell != null)
            {
                firstCell.SetValue(Grid.ColumnProperty, e.Column.DisplayIndex);
            }
        }
  
        private void GridViewScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            this.filteringRow.ScrollViewer.ScrollToHorizontalOffset(e.HorizontalOffset);
        }
  
        private void FilterCell_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            ColumnFilterCell columnHeader = (ColumnFilterCell)sender;
            RadGridView radGridView = columnHeader.ParentOfType<RadGridView>();
  
            if (e.PropertyName == "IsFilterActive")
            {
                if (columnHeader.IsFilterActive)
                {
                    radGridView.FilterDescriptors.Add(columnHeader.FilterDescriptor);
                }
                else
                {
                    radGridView.FilterDescriptors.Remove(columnHeader.FilterDescriptor);
                }
            }
        }
  
        private int NumberOfColumnsInGridView(RadGridView gridView)
        {
            int number = 0;
            foreach (GridViewColumn column in gridView.Columns)
            {
                number++;
            }
            return number;
        }
    }
}

2 Answers, 1 is accepted

Sort by
0
Nicole
Top achievements
Rank 1
answered on 05 Feb 2011, 06:04 PM
I think I am getting a bit closet to the problem, I was looking at the wrong place in my initial query. After the user has pressed Enter, I am calculating which filter operator will be needed for the users entry. And I can see that each time the filte roperator changes I get no results first. THe next time I press on Enter it works fine. Do I have to initialize something when the filteroperator changes?

this is my code:

private void FilterTextBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        ActivateFiltering();
    }
}
private void ActivateFiltering()
{  
    string filterTextWithAsterisk = this.FilterTextBox.Text;
    FilterOperator filterOperator = GetMatchingFilterOperator(filterTextWithAsterisk);
    this.FilterDescriptor.Operator = filterOperator;
    BindingExpression exp = this.FilterTextBox.GetBindingExpression(TextBox.TextProperty);
    exp.UpdateSource();
    this.IsFilterActive = true;
      
}
 
0
Vlad
Telerik team
answered on 10 Feb 2011, 09:07 AM
Hello,

 Unfortunately it will be very hard to help you with the provided information so far. Can you send us working example where we can check what's going on? Once we have more info we will let you know about our findings. 

Greetings,
Vlad
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
Tags
GridView
Asked by
Nicole
Top achievements
Rank 1
Answers by
Nicole
Top achievements
Rank 1
Vlad
Telerik team
Share this question
or