Sorting and filtering not working with generic types

8 posts, 0 answers
  1. Dieter
    Dieter avatar
    3 posts
    Member since:
    Mar 2012

    Posted 02 Mar 2012 Link to this post

    Hi,

    I have made a dynamic grid which is populated by a viewmodel 'DynamicGridViewModel'

    public class DynamicGridViewModel: BaseViewModel
    {
      public DynamicGridViewModel(Func<DetailedObservableCollection<DynamicGridColumnViewModel>> getColumns,
        Action getData)
      {
        GetColumns = getColumns;
        GetData = getData;
          
        Columns = GetColumns();
      }
     
      private readonly Func<DetailedObservableCollection<DynamicGridColumnViewModel>> GetColumns;
      public Action GetData;
     
      public void SetColumns()
      {
        GetColumns();
        if (Columns.Any(dynamicGridColumnViewModel => dynamicGridColumnViewModel.IsFixedColumn
                                                      && Columns.IndexOf(dynamicGridColumnViewModel) > 0
                                                      && !Columns[Columns.IndexOf(dynamicGridColumnViewModel) - 1].IsFixedColumn))
        {
          throw new Exception("Fixed (frozen) columns should appear on the beginning of the grid");
        }
      }
     
      private DetailedObservableCollection<DynamicGridColumnViewModel> _columns;
      public DetailedObservableCollection<DynamicGridColumnViewModel> Columns
      {
        get { return _columns; }
        set
        {
          if (value != _columns)
          {
            _columns = value;
            RaisePropertyChanged(() => Columns);
          }
        }
      }
     
      private DetailedObservableCollection<DynamicGridRowViewModel> _rows = new DetailedObservableCollection<DynamicGridRowViewModel>();
      public DetailedObservableCollection<DynamicGridRowViewModel> Rows
      {
        get { return _rows; }
        set
        {
          if (value != _rows)
          {
            _rows = value;
            RaisePropertyChanged(() => Rows);
          }
        }
      }
         
         
     
    }

    public abstract class DynamicGridColumnViewModel : BaseViewModel
    {
      private string _header;
      public string Header
      {
        get { return _header; }
        set
        {
          if (value != _header)
          {
            _header = value;
            RaisePropertyChanged(() => Header);
          }
        }
      }
     
     
      private bool _isFixedColumn;
      public bool IsFixedColumn
      {
        get { return _isFixedColumn; }
        set
        {
          if (value != _isFixedColumn)
          {
            _isFixedColumn = value;
            RaisePropertyChanged(() => IsFixedColumn);
          }
        }
      }
     
      public abstract Type GetColumnValuesType();
    }
    }
     
    [DebuggerDisplay("{Header}")]
    public class DynamicGridColumnViewModel<T> : DynamicGridColumnViewModel
    {
      
     
      public override Type GetColumnValuesType()
      {
        return typeof(T);
      }
    }

    public class DynamicGridRowViewModel: BaseViewModel
    {
      public DynamicGridRowViewModel()
      {
        Cells=new DetailedObservableCollection<DynamicGridCellViewModel>();
      }
     
      public DetailedObservableCollection<DynamicGridCellViewModel> Cells { get; private set; }
    }

    [DebuggerDisplay("{Value}")]
    public class DynamicGridCellViewModel : BaseViewModel
    {
    }
     
    [DebuggerDisplay("{Value}")]
    public class DynamicGridCellViewModel<T> : DynamicGridCellViewModel
    {
      public DynamicGridCellViewModel(T value)
      {
        Value = value;
      }
     
      private T _value;
      public T Value
      {
        get { return _value; }
        private set
        {
          if (!value.Equals(_value))
          {
            _value = value;
            RaisePropertyChanged(() => Value);
          }
        }
      }
    }


    In my view in code-behind I create the columns for the view. Because the value-type I am binding to is a generic type, I also set the DataType for the columns in code-behind.

    public partial class OverzichtView: UserControl
    {
      public OverzichtView()
      {
        InitializeComponent();
        Loaded += OverzichtViewLoaded;
      }
     
      private void OverzichtViewLoaded(object sender, RoutedEventArgs e)
      {
        BuildColumns();
     
        var vm = OverzichtGridView.DataContext as DynamicGridViewModel;
        if (vm != null)
          vm.GetData();
      }
     
      private void BuildColumns()
      {
     
        var vm = OverzichtGridView.DataContext as DynamicGridViewModel;
     
        OverzichtGridView.Columns.Clear();
        OverzichtGridView.FrozenColumnCount = 0;
     
        if (vm != null)
        {
          OverzichtGridView.ItemsSource = vm.Rows;
     
          foreach (var column in vm.Columns)
          {
     
            var  gridColumn = new GridViewDataColumn();
     
            gridColumn.Header = column.Header;
            gridColumn.DataMemberBinding = new Binding
                                             {
                                               Path =
                                                 new PropertyPath("Cells[" + vm.Columns.IndexOf(column) + "].Value"),
                                               Mode = BindingMode.TwoWay
                                             };
            gridColumn.DataType = column.GetColumnValuesType();
             
            OverzichtGridView.Columns.Add(gridColumn);
            if (column.IsFixedColumn)
              OverzichtGridView.FrozenColumnCount++;
          }
        }
      }
    }


    This is working fine until I try to use the default interactive sorting and filtering functions on the grid. When I click on a header, the arrow that is shown when a column is sorted appears, however the data is not sorted. When I click the filter-button in the column-header, the distinct values aren't present in the filter-view and when I apply a custom filter, the data is not filtered.
    I suspect it has something to do with the data-type. Do I have to set another property for the column? I compared with a gridview that is binding to a string-property and the only difference I can find is that with the generic type, the ItemPropertyInfo is not set (but I can't change it in my example because it is read-only.
    Can you help me on this matter please?

    Kind regards,
    Dieter
  2. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 07 Mar 2012 Link to this post

    Hello,

     Can you verify if your scenario will work normally with standard Silverlight DataGrid?

    Kind regards,
    Vlad
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  3. Dieter
    Dieter avatar
    3 posts
    Member since:
    Mar 2012

    Posted 20 Mar 2012 Link to this post

    Hello Vlad,

    as far as I know there are no standard interactive sorting and filtering possibilities in the standard Silverlight Datagrid. If there are, please let me know and I will test it first.

    I have a zip file with a small sample solution, but I can't seem to post it here?

    kind regards,
    Dieter
  4. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 20 Mar 2012 Link to this post

    Hi,

     Sorting for standard Silverlight DataGrid is similar to RadGridView - click on the column header. You can open support ticket to attach the project. 

    Regards,
    Vlad
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  5. Dieter
    Dieter avatar
    3 posts
    Member since:
    Mar 2012

    Posted 20 Mar 2012 Link to this post

    Hello,

    I tried it with the standard SL datagrid and sorting also didn't work. Any idea what the problem is and how to solve it?

    kind regards,
    Dieter
  6. David
    David avatar
    4 posts
    Member since:
    Mar 2012

    Posted 27 Mar 2012 Link to this post

    Hey guys, 

    I have exactly the same scenario my end.  I am dynamically adding a column and specifying the binding and the column wont sort.  The sort icons appear no physical sort of the data takes place.  

    Is there any update on this issue? 
  7. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 27 Mar 2012 Link to this post

    Hello,

     If the standard Silverlight DataGrid cannot sort in your case as well I suggest you to check your data layer implementation. 

    All the best,
    Vlad
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  8. David
    David avatar
    4 posts
    Member since:
    Mar 2012

    Posted 27 Mar 2012 Link to this post

    So I found the solution to my problem.  Basically this code which defines the DisplayMemeberBinding was the same as my code. 

    new PropertyPath("Cells[" + vm.Columns.IndexOf(column) + "].Value")

    The problem was that the Cells collection on my VM was exposed as an ICollection and the implementation was an ObservableCollection.  I swapped this out to a standard array and then the sorting starts to work.  So it must be an issue with the collection type that is being used. 

Back to Top