Sorting issue

Aug 28, 2008 at 4:09 PM
Hi,

 Here is my scenario (very simplified):
 
 I have a collection of objects that look like this:
 Customer: FirstName, LastName, Address,
 Where Address is a object with Street and Number

 Here is a column definition:
<toolkit:DataGridTextColumn DataFieldBinding="{Binding Address.Street}" Header="Street" SortMemberPath="Address.Street"/>

Now, sorting will not work on this. Is there any work around for this ? Or am I doing something wrong here ?

Thanks,
Calin


Coordinator
Aug 28, 2008 at 6:47 PM
I tried it out with this:

public

 

class Address
{
public string City { get; set; }
public string State { get; set; }
}

 

 

public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
}

 

<

 

dg:DataGridTextColumn Header="City" DataFieldBinding="{Binding Path=Address.City}" />
<dg:DataGridTextColumn Header="City" DataFieldBinding="{Binding Path=Address.City}" SortMemberPath="Address.City" />

 


and I didn't have any problems sorting with either columns.  Is your customer object coming from a DataTable or XmlDataProvider?
Aug 28, 2008 at 7:44 PM
Hi,

 I am getting my data from a table mapped with Linq to sql, maybe that is the issue.

Regards,
Aug 29, 2008 at 6:49 AM
Here is the error I get:

System.InvalidOperationException: Failed to compare two elements in the array. ---> System.ArgumentException: Object must be of type String.\r\n   at System.String.CompareTo(Object value)\r\n   at System.Collections.Comparer.Compare(Object a, Object b)\r\n   at MS.Internal.Data.SortFieldComparer.Compare(Object o1, Object o2)\r\n   at System.Array.SorterObjectArray.SwapIfGreaterWithItems(Int32 a, Int32 b)\r\n   --- End of inner exception stack trace ---\r\n   at System.Array.SorterObjectArray.SwapIfGreaterWithItems(Int32 a, Int32 b)\r\n   at System.Array.SorterObjectArray.QuickSort(Int32 left, Int32 right)\r\n   at System.Array.Sort(Array keys, Array items, Int32 index, Int32 length, IComparer comparer)\r\n   at System.Collections.ArrayList.Sort(Int32 index, Int32 count, IComparer comparer)\r\n   at System.Collections.ArrayList.Sort(IComparer comparer)\r\n   at System.Windows.Data.ListCollectionView.PrepareLocalArray(IList list)\r\n   at System.Windows.Data.ListCollectionView.RefreshOverride()\r\n   at System.
Windows.Data.CollectionView.RefreshInternal()\r\n   at System.Windows.Data.CollectionView.RefreshOrDefer()\r\n   at System.Windows.Data.ListCollectionView.SortDescriptionsChanged(Object sender, NotifyCollectionChangedEventArgs e)\r\n   at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)\r\n   at System.ComponentModel.SortDescriptionCollection.OnCollectionChanged(NotifyCollectionChangedAction action, Object item, Int32 index)\r\n   at System.ComponentModel.SortDescriptionCollection.InsertItem(Int32 index, SortDescription item)\r\n   at System.Windows.Controls.ItemCollection.SynchronizeSortDescriptions(NotifyCollectionChangedEventArgs e, SortDescriptionCollection origin, SortDescriptionCollection clone)\r\n   at System.Windows.Controls.ItemCollection.SortDescriptionsChanged(Object sender, NotifyCollectionChangedEventArgs e)\r\n   at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionC
hangedEventArgs e)\r\n   at System.ComponentModel.SortDescriptionCollection.OnCollectionChanged(NotifyCollectionChangedAction action, Object item, Int32 index)\r\n   at System.ComponentModel.SortDescriptionCollection.InsertItem(Int32 index, SortDescription item)\r\n   at System.Collections.ObjectModel.Collection`1.Add(T item)\r\n   at Microsoft.Windows.Controls.DataGrid.DefaultSort(DataGridColumn column, Boolean clearExistingSortDescriptions) in E:\\Proiecte\\KammerDiener\\WPFToolkit\\DataGrid\\Microsoft\\Windows\\Controls\\DataGrid.cs:line 5014"

Coordinator
Aug 29, 2008 at 3:08 PM
Could you post the entity code that you are using?
Aug 29, 2008 at 6:44 PM
Hi Vinsibal,

 I made a example for you using Adventurework database, I've added 2 table Product and ProductSubcategory
 
 A simple window1.xaml

<toolkit:DataGrid Name="dgProducts" AutoGenerateColumns="False">
            <toolkit:DataGrid.Columns>
                <toolkit:DataGridTextColumn DataFieldBinding="{Binding Name}" />
                <toolkit:DataGridTextColumn DataFieldBinding="{Binding ProductSubcategory.Name}" />
            </toolkit:DataGrid.Columns>
        </toolkit:DataGrid>

And here is the code in ctor:

Variant I:
AdventureWorksDataContext dataContext = new AdventureWorksDataContext();
            dgProducts.SetBinding(DataGrid.ItemsSourceProperty, new Binding() { Source = dataContext.Products });
does not sort when I click the column headers

Variant II:
AdventureWorksDataContext dataContext = new AdventureWorksDataContext();
            dgProducts.SetBinding(DataGrid.ItemsSourceProperty, new Binding() { Source = dataContext.Products.ToList() });
Does sort on the first column (name).

Hope this helps,

Regards,
Calin,
Aug 29, 2008 at 6:58 PM
I figured out what the problem is.

IQueryable<Product> productsNotNull = dataContext.Products.Where(p => p.ProductSubcategory != null).Take(10);
dgProducts.SetBinding(DataGrid.ItemsSourceProperty, new Binding() { Source = productsNotNull.ToList() });

Sorting works like a charm.

IQueryable<Product> productNull = dataContext.Products.Where(p => p.ProductSubcategory == null).Take(10);
dgProducts.SetBinding(DataGrid.ItemsSourceProperty, new Binding() { Source = productNull.ToList() });

Does not work.

So the issue is null cells, the actual exception is "Object must be of type String.". All it remains now is to figure out a solution.
Aug 13, 2010 at 7:22 AM

Hi i run into the same exception, is there already a solution for this problem. I don't really know what to do, please give some hints if there exists some.

Jan 1, 2011 at 11:56 AM

 I  run into the same exception, is there already a solution for this problem. I don't really know what to do, please give some hints if there exists some.