Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums

Go Back   Vista Forums > Vista technology newsgroups > WinFX General

Sorting a ListView

Reply
 
Thread Tools Display Modes
Old 03-31-2008   #1 (permalink)
escher4096
Guest
 
Posts: n/a

Sorting a ListView

I am attempting to sort the ItemsCollection of a ListView in C# / WPF.
I have it all hooked up and for the base case it is working fine. Base
case assumes binding to a single datatable in a dataset:

/// there is a bunch of other code before this but it all just
sets up for this
/// one method which actually does the sorting

/// <summary>
/// Does the column sorting
/// </summary>
/// <param name="columnBindToPropertyToSortOn">
/// Which property should we sort on</param>
/// <param name="direction">
/// What direction should we sort on</param>
protected virtual void Sort(string
columnBindToPropertyToSortOn, ListSortDirection direction)
{
if (null != columnBindToPropertyToSortOn)
{
Items.SortDescriptions.Clear();
SortDescription sd = new
SortDescription(columnBindToPropertyToSortOn, direction);
Items.SortDescriptions.Add(sd);
Items.Refresh();
}
}

This works perfectly if we want to sort by exactly what is in the
table. But I have 2 cases where this doesn't make sense.

Case 1: Where we are storing GUIDs in the bound table to our look up
table and displaying the value through a converter or combo box (where
the combo box item source is bound to the lookup table and selected
value is bound to the GUID in our current table). Sorting by a GUID,
while fun, rarely results in a nice alphabetic sorting. I need to sort
by the value we get from the converter/display value in the combobox,
not the GUID we are storing in the table. Since we are allowing the
user to edit the values in the list view (through the combobox) we
can't just do the conversion from GUID to pretty name in the SQL
before displaying the results.

Case 2: Binding through related data. If we have 4 columns in our list
and:
column 1 is bound to table 1 / column 1
column 2 is bound to table 1 / column 2
column 3 is bound to table 1 / some relation to table 2 / column
1
column 4 is bound to table 1 / some relation to table 2 / some
relation in table 3 / column 2

The display is fine and sorting works for the first 2 columns
but the sorting borks on column 3 and 4 because the column isn't in
the current table / view.

I have looked at building a custom 'SortDescription' class but it is a
struct (which means no inheritance) and does have an interface. I have
not figured out where in the ItemsCollection the sort actually happens
(thinking that maybe I could write an extension method or inherit from
it and override some method).

I have attempted to sort the items in ListView.Items manually but of
course you can't do that because the items belong to the underlying
view to which we don't have access. We can't pass in an ICompare or
anything like that. The only way to sort the collection that I can
find (SortDescription) does not allow you to do anything funky.

Any help would be appreciated.

Thanks

-Cam
  Reply With Quote

Reply

Thread Tools
Display Modes









Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48