Grid Sorting with multiple object types

Jul 9, 2009 at 9:29 PM


I am trying to Sort a DataGrid that has anunderlying collection with different objects defined in it.  Here is my example..

I have three types of objects.
 - Card (Base Class that all my other classes implement).  Card has four properties, ID, Name, Text, and Type
 - ItemCard (implements Base class of Card).  ItemCard has the base properties and two new ones of Cost and Strength
 - RegionCard (implements Base class of Card).  RegionCard has base properties and one new one of Location.

I created a CollectionViewSource, and load my cards into it:
 - CardList.Source = LoadCards();  (this method returns an ObservableCollection<Card> that contains all my cards loaded from an XML file)

Finallyl, I bind this Collection to my DataGrid:
 - CardGrid.DataContext = CardList.View

At this point, my cards will display in the DataGrid with columns for each of the Base card properties, and columns for each of the specific card properties.  The Grid can be Filtered just fine too.  If I try to Sort on one of the Base Card properties, it will work, since all the Cards in the DataGrid have that property (i.e Sorting on Name or Text works fine).  If I try to Sort on Cost, Strength, or Location though, I get an error. 

My Question is...  How do I sort this grid on the specific card properties (Cost, Strength, Location)?


Jul 14, 2009 at 2:51 PM

Bumpity bump..  Anyone have any thoughts on this?  I am still puzzling over how to accomplish sorting of multiple object types in a DataGridView.