View previous topic :: View next topic |
Author |
Message |
jkempner
Joined: 03 Sep 2008 Posts: 17
|
Posted: Fri Sep 07, 2012 7:17 pm Post subject: SortableBindingList problems |
|
|
I have a component class that I've derived from VirtualTree that I then use in a custom control. In the control, I set the VirtualTree's DataSource to a SortableBindingList<T>, and set the TypedListName to the name of the class that I use for T, but none of the columns will sort. Are there any other things I need to do (or avoid doing) to get this to work? Are there overrides I should avoid in the component that I've derived from VirtualTree that could interfere with sorting? |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Sat Sep 08, 2012 1:01 am Post subject: |
|
|
This can be a little tricky to set up correctly.
Lets say your object class is called MyObj defined in namespace MyNs and your derived list class is called MyList.
You need to define two object Row bindings in the Virtual Tree designer Data Binding tab. The first is an object binding for the derived list class you are going to set as datasource. This binding should have:
TypedListName:
TypeName: MyNs.MyList
ChildProperty: this
This binding doesn't require any CellBindings unless you have ShowRootRow set to true and want to display text corresponding to the root row.
The second binding is for MyObj. This should have:
TypedListName: MyObj
TypeName:
You then add CellBindings for each property of MyObj that you want to display in a column. _________________ Infralution Support |
|
Back to top |
|
|
jkempner
Joined: 03 Sep 2008 Posts: 17
|
Posted: Mon Sep 10, 2012 3:05 pm Post subject: |
|
|
I don't actually have a derived list class. I was just trying to use a SortableBindingList<MyObj> as the DataSource.
Do I need to create a new class that has a SortableBindingList<MyObj> in order to get this to work? |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Mon Sep 10, 2012 10:50 pm Post subject: |
|
|
If you are binding to a generic list then you will either need to derive a new class for the list eg
Code: | class MyList : SortableBindingList<MyObj>
{} |
or else you can handle the GetBinding event to programmatically set the row binding for the given objects. _________________ Infralution Support |
|
Back to top |
|
|
jkempner
Joined: 03 Sep 2008 Posts: 17
|
Posted: Wed Nov 07, 2012 1:06 pm Post subject: |
|
|
Thanks, that did the trick.
A follow-up question: is there a way to specify a secondary sort column when using the automatic sorting provided by this mechanism? In other words, if I'm sorting on column A, can I specify a second column where Compare can be used to break ties in column A's sorting?
Thanks,
Josh |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Wed Nov 07, 2012 9:48 pm Post subject: |
|
|
There is no mechanism to do this dynamically (ie allowing the user to select the secondary sort field) however you can program a secondary sort field for a given primary field by deriving from your derived SortableBindingList class and then overriding the Compare method.
The default implementation of this method is shown below. You would need to change this to handle specific cases.
Code: | protected virtual int Compare(T lhs, T rhs)
{
int result = 0;
object lhsValue = (lhs == null) ? null : SortPropertyCore.GetValue(lhs);
object rhsValue = (rhs == null) ? null : SortPropertyCore.GetValue(rhs);
if (lhsValue == null)
{
// both null => 0
// lhs null, rhs non-null => -1
result = (rhsValue == null) ? 0 : -1;
}
else if (rhsValue == null)
{
// lhs non-null, rhs null = > 1
result = 1;
}
else if (lhsValue is IComparable)
{
// use IComparable if possible
//
result = ((IComparable)lhsValue).CompareTo(rhsValue);
}
else if (lhsValue.Equals(rhsValue))
{
// check for equality
//
result = 0;
}
else
{
// if all else fails compare as strings
//
result = lhsValue.ToString().CompareTo(rhsValue.ToString());
}
if (_sortDirection == ListSortDirection.Descending)
result = -result;
return result;
} |
_________________ Infralution Support |
|
Back to top |
|
|
jkempner
Joined: 03 Sep 2008 Posts: 17
|
Posted: Thu Nov 08, 2012 8:49 pm Post subject: |
|
|
Thanks, that works perfectly. |
|
Back to top |
|
|
|