How to wrap a DataMember list with a SortedBindingList

How to wrap a DataMember list with a SortedBindingList

Old forum URL: forums.lhotka.net/forums/t/2698.aspx


Jimbo posted on Saturday, April 14, 2007

Can someone show how we wrap a DataMember of a parent BindingSource with a SortedBindingList ?
mRootList
    RootListItem
        pChildList (prop)

It is straight forward to wrap and bind a root list to a binding source with a SortedBindingList (as for example see PTWin ProjectList) as it is strongly typed.

RootListBindingSource.DataSource = New SortedBindingList(of myRootListType) (mRootList)

However when it comes to a list that is a child in a Parent-Child relationship the issue is different because the DataMember property of the BindingSource requires a string literal.

ChildListBindingSource.DataSource = RootListBindingSource
ChildListBindingSoutce.DataMember = "pChildList" 

Jimbo





Jimbo replied on Sunday, April 22, 2007

Is this question too hard ? or am I missing something ?

Jimbo

xal replied on Sunday, April 22, 2007

If I remember correctly, the binding source has an InnerList property (or something along those lines).
You can use that in order to get the actual list.

Also, you don't necessarily need to use the main binding source as datasource and specify a datamember. You can do this:

childBindingSource.DataSource = New SortedBindingList(Of Child)(mRootObject.ChildList)

As a last resort, you could create a property such as this inside your root object:

    <NonSerialized(), NotUndoable()> _
    Private m_SortedChildren As SortedBindingList(Of MyChild)
    Public ReadOnly Property SortedChildren() As SortedBindingList(Of MyChild)
        Get
            If m_SortedChildren Is Nothing Then
                m_SortedChildren = New SortedBindingList(Of MyChild)(Me.m_Children)
                m_SortedChildren.ApplySort("SomeField", _
                    ComponentModel.ListSortDirection.Descending)
            End If
            Return m_SortedChildren
        End Get
    End Property



That way, you can use that "SortedChildren" property as a datamember and that would be it. This is particularily useful for grandchildren, where you'd have to reassign the granchild bindingsource's datasource whenever the "current child" changes...

Some could argue that this is suboptimal because these lists should be part of the ui, but hey, sometimes you just have to get the job done!! Wink [;)]

Andrés

Jimbo replied on Wednesday, May 23, 2007

Thanks Andres,

I use your first solution quite often, but in this case the second idea is the best.

Prior to using csla and also now I have been doing much like you suggest in the second solution - ie have a pre-wrapped sortable list object exposed through a readonly property.

Horses for courses.

Regards,
Jimbo

Copyright (c) Marimer LLC