EditLevel Error with Linq SortEditLevel Error with Linq Sort
Old forum URL: forums.lhotka.net/forums/t/6976.aspx
Topher posted on Wednesday, May 20, 2009
WinForms
CSLA 3.6.2
.NET 3.5 SP2
I've been trying to sort a child collection on a column header click in a DataGridView using Linq. I can get the the collection to sort, but I'm unable to edit the child collection due to an EditLevel error. The child collection edits fine if I don't try to apply sorting.
I have a pretty simple object graph: Parent -> ChildCollection -> Child. The parent and child inherit from BusinessBase, the ChildCollection from BusinessListBase. I'm using a CslaActionExtender control to handle the windows databinding. The form has various controls which have the following pertinent settings:
ParentBS.DataSource = Parent
ChildCollectionBS.DataSource = ParentBS
ChildCollectionBS.DataMember = ParentBS.ChildCollectionProperty
ChildCollectionDGV.DataSource = ChildCollectionBS
CslaActionExtender.DataSource = ParentBS
On form load I'm populating and sorting my collection like this:
Dim _sortDirection As SortOrder = SortOrder.Ascending
Private Sub FillForm()
_parent = Parent.GetParent()
CslaActionExtenderSubscriber.ResetActionBehaviors(_parent)
ReadWriteAuthorization1.ResetControlAuthorization()
ChildCollectionBS.DataSource = _parent.ChildCollectionProperty.OrderBy(String.Format("ChildID {0}", _sortDirection.ToString))
End Sub
And I'm handling the ColumnHeaderMouseClick event to enable sorting like this:
Private Sub ChildCollectionDGV_ColumnHeaderMouseClick(...) Handles ChildCollectionDGV.ColumnHeaderMouseClick
_sortDirection = IIf(_sortDirection = SortOrder.Ascending, SortOrder.Descending, SortOrder.Ascending)
Dim columnName As String = ChildCollectionDGV.Columns(e.ColumnIndex).DataPropertyName
ChildCollectionBS.DataSource = _parent.ChildCollectionProperty.OrderBy(String.Format("{0} {1}", columnName, _sortDirection.ToString))
ChildCollectionDGV.Columns(e.ColumnIndex).HeaderCell.SortGlyphDirection = _sortDirection
End Sub
I've seen Linq sort examples in the other forums, but I feel like I'm missing something. Can someone point me in the right direction so I can sort and edit the child collection?
Thanks!
Topher
JonnyBee replied on Sunday, June 07, 2009
Hi,
I prefer to use SortedBindingList and FilteredBindingList when programming for WindowsForms.
Unbind/Rebind of bindingsources are quite "touchy" (tpo make sure you exit edit mode properly, calling either ApplyEdit og CancelEdit before unbind)
/jonny
RockfordLhotka replied on Sunday, June 07, 2009
The problem is that Windows Forms pre-dates LINQ, and they really aren't a great match.
While you can write a bunch of code to coerce LINQ to work with Windows Forms (and people do this), I agree that it is better to use technologies and solutions geared toward Windows Forms.
SortedBindingList and FilteredBindingList require FAR less code and naturally integrate with Windows Forms in ways LINQ probably never will (because Microsoft has quit updating Windows Forms in any meaningful way).
Copyright (c) Marimer LLC