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

Sorting and filtering not working with generic types

7 Answers 128 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dieter
Top achievements
Rank 1
Dieter asked on 02 Mar 2012, 04:09 PM
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

7 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 07 Mar 2012, 03:50 PM
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 >>
0
Dieter
Top achievements
Rank 1
answered on 20 Mar 2012, 03:22 PM
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
0
Vlad
Telerik team
answered on 20 Mar 2012, 03:28 PM
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 >>
0
Dieter
Top achievements
Rank 1
answered on 20 Mar 2012, 04:06 PM
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
0
David
Top achievements
Rank 1
answered on 27 Mar 2012, 11:16 AM
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? 
0
Vlad
Telerik team
answered on 27 Mar 2012, 12:07 PM
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 >>
0
David
Top achievements
Rank 1
answered on 27 Mar 2012, 03:33 PM
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. 

Tags
GridView
Asked by
Dieter
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Dieter
Top achievements
Rank 1
David
Top achievements
Rank 1
Share this question
or