Home Forums WPF controls Xceed DataGrid for WPF Sort issue using non-native data types

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #24661 |

    I am binding to a data source by using DataControlGrid.ItemsSource. Unfortunately I am not using a DataGridCollectionView, but I am using an observable collection. The observable collection is of a class that I have created, containing properties for native and non-native complex data types.

    I have managed to get the data to show and the grid works great for me to add, remove, and select rows. The only problem is when I sort. If I select a column to sort by clicking on the column title, the grid appears to do a sort. However, when the rows are sorted, the data is not sorted in any logical order. I can click the title to do an ascending or descending sort, but the grid shows up in the exact same random order between the two. When I turn the sort off, the grid goes back to the order the items were added into the collection. The sorts work just fine on native data types (double, int, string, etc), but not on data types that I created (implementing the ICompare interface).

    The following is a very simplified version of how my code looks:

    public class GridSource
    {
    public Type1 TypeOne { get; set; }
    public Type2 TypeTwo { get; set; }
    public double DoubleOne { get; set; }
    public double DoubleTwo { get; set; }
    public Type3 TypeThree { get; set; }
    }

    /// The following is done in the constructor for the window XAML .cs
    ObservableCollection<GridSource> gridCollection = new ObservableCollection<GridSource>();

    // “grid” is defined in XAML as the following:
    //<xcdg:DataGridControl x:Name=”grid” ItemScrollingBehavior=”Immediate” />

    grid.View.FixedHeaders.RemoveAt(0); //Remove “Grouping” bar from the header.

    grid.AutoCreateColumns = false;
    grid.ItemsSource = gridCollection;
    grid.SelectionMode = SelectionMode.Single; //Allow only single row selections
    grid.NavigationBehavior = NavigationBehavior.RowOnly;

    // Create and Bind the data columns to the observable collection.
    grid.Columns.Add(new Column(“TYPEONE”, “Type One”, new Binding(“TypeOne”)));
    grid.Columns.Add(new Column(“TYPETWO”, “Type Two”, new Binding(“TypeTwo”)));
    grid.Columns.Add(new Column(“DOUBLEONE”, “Double One”, new Binding(“DoubleOne”)));
    grid.Columns.Add(new Column(“DOUBLETWO”, “Double Two”, new Binding(“DoubleTwo”)));
    grid.Columns.Add(new Column(“TYPETHREE”, “Type Three”, new Binding(“TypeThree”)));

    grid.PropertyChanged += new PropertyChangedEventHandler(grid_PropertyChanged);

    Any help that anyone can offer would be tremendously appreciated.
    Thanks!!!

    Imported from legacy forums. Posted by Joe (had 697 views)

    Xceed Support
    Member
    Post count: 5658

    I was intrigued by this and digged a little deeper and noticed that the ListCollectionView used automatically to wrap your ObservableCollection<T> will feed its own Sort Comparer to the Array.Sort() method… Therefore the behavior you are seeing is “normal” and not really avoidable.

    Imported from legacy forums. Posted by Marcus [Xceed] (had 2627 views)

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.