Something has changed in the latest version (2011.2.920.1040) of the GridView in handling programatic sorting. Let me start with the white screen exception then explain how we add the sort descriptor:
Error: Unhandled Error in Silverlight Application
Code: 4004
Category: ManagedRuntimeError
Message: System.NullReferenceException: Object reference not set to an instance of an object.
at Telerik.Windows.Data.Expressions.ExpressionFactory.MakeMemberAccess(Expression instance, String memberName, Type memberType)
at Telerik.Windows.Data.Expressions.PropertyAccessExpressionBuilder.CreateMemberAccessExpressionOverride()
at Telerik.Windows.Data.Expressions.MemberAccessExpressionBuilderBase.CreateMemberAccessExpression()
at Telerik.Windows.Controls.GridViewColumn.CreateSortKeyExpression(ParameterExpression parameterExpression, ExpressionBuilderOptions options)
at Telerik.Windows.Controls.GridView.ColumnSortDescriptor.CreateSortKeyExpression(ParameterExpression parameterExpression)
at Telerik.Windows.Data.SortDescriptorBase.CreateSortKeyExpression(Expression itemExpression)
at Telerik.Windows.Data.SortDescriptorExtensions.GetSortKeyLambda(ISortDescriptor sortDescriptor, Type itemType)
at Telerik.Windows.Data.QueryableCollectionView.InitializeInternalList(IQueryable view)
at Telerik.Windows.Data.QueryableCollectionView.CreateInternalList()
at Telerik.Windows.Data.QueryableCollectionView.get_InternalList()
at Telerik.Windows.Data.QueryableCollectionView.GetEnumerator()
at Telerik.Windows.Data.DataItemCollection.GetFlatEnumerator()
at Telerik.Windows.Data.DataItemCollection.GetEnumerator()
at Telerik.Windows.Controls.GridView.GridViewVirtualizingPanel.EnsureItems()
at Telerik.Windows.Controls.GridView.GridViewVirtualizingPanel.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)
Our system is set up to at times override the default sorting set up a domain data source if the user so chose to. On a first load, say we sort by a column called 'code.' During the use of the page, the user can choose to resort the grid with a different column. When they leave the page, during the unloaded event we then capture which sort they put into place and store it in app resources. If they navigate back to the page, we then restore those custom sort descriptors programatically. Here is where that issue above comes into place.
The code to restore the setting (after retreiving it from app resources) - I've set this code up with hard coded strings to explain the example after:
dds.SortDescriptors.Add(
New
Windows.Controls.SortDescriptor(
"EntityB.Description"
, ListSortDirection.Ascending))
Dim
columnSort
As
New
Telerik.Windows.Controls.GridView.ColumnSortDescriptor()
columnSort.Column = grd.Columns(
"EntityBDescription"
)
columnSort.SortDirection = ListSortDirection.Ascending
grd.SortDescriptors.Add(columnSort)
Domain data source is set up for AutoLoad to false and after the code runs above, it is programatically changed to true but after the code above has completed.
Because we are using a Domain Data Source (DDS), we found that the sort must be set up on the DDS first then the grid. At times, however, that sort path may be different for each; specifically when trying to sort via a projected property (i.e. a property from a reference property of the entity being bound). If Entity A has a relationship to Entity B, Entity Framework obviously sets up a property on Entity A called 'Entity B.' Using include statements, we set up a projected property for Entity B's Description field called "EntityBDescription" which will include this property directly to Entity A for display. The problem is the actual sort path would be "EntityB.Description" for the DDS but "EntityBDescription" for the grid. This approach worked perfectly on the GridView until now.
On the return visit, the restore code above produces the exception that I listed and I'm unsure as to why.