RadGridView throws exception when clicking on a row if GroupDescriptors have been added
-
25-10-2019 - |
문제
My RadGridView is throwing an System.InvalidCastException when there are GroupDescriptors applying a grouping to the items in the grid.
The exception is fairly unhelpful:
[A]DynamicDataType cannot be cast to [B]DynamicDataType. Type A originates from 'DynamicData, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' in a byte array. Type B originates from 'DynamicData, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' in a byte array.
at Telerik.Windows.Data.FuncExtensions.<>c__DisplayClass1`2.<ToUntypedFunc>b__0(Object item)
at Telerik.Windows.Data.QueryableCollectionViewGroup.FindLastLevelGroupByItem(Object item)
at Telerik.Windows.Controls.GridView.GridViewDataControl.GetRowForItem(Object item, Boolean forceGroupExpand)
at Telerik.Windows.Controls.GridView.GridViewDataControl.get_CurrentCell()
at Telerik.Windows.Controls.GridView.GridViewDataControl.CanCellBecomeCurrent(GridViewCell cell)
at Telerik.Windows.Controls.GridView.GridViewCell.OnMouseLeftButtonDown(MouseButtonEventArgs e)
at System.Windows.Controls.Control.OnMouseLeftButtonDown(Control ctrl, EventArgs e)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
When the groups are removed from the bar at the top of the grid, there is no problem and I can select the rows of the grid as per normal.
The problem perhaps appears to be related to a piece of code that I use to save the GridDescriptors between refreshes. The method takes in RefreshItemSourceLifetimeStage which is an enum. This code is called before (AboutToRefresh) and after (FinishedRefresh) the ItemSource is changed with the appropriate argument.
public void RefreshGrid(RefreshItemSourceLifetimeStage stage)
{
switch (stage)
{
case RefreshItemSourceLifetimeStage.AboutToRefresh:
if (_grid.ItemsSource == null && !_grid.GroupDescriptors.Any())
{
// default group descriptor that you get on initial page load
_savedGroupDescriptors.Add(new GroupDescriptor { Member = "_CallFactorSet.CreatedAt.Year", DisplayContent = "Year Created", SortDirection = ListSortDirection.Descending});
}
else
{
_savedGroupDescriptors.Clear();
foreach (var igd in _grid.GroupDescriptors)
{
// we have to clone the objects into the _savedGroupDescriptors collection because otherwise they disappear when the itemsource is refreshed
if (igd is GroupDescriptor)
{
var gd = (GroupDescriptor) igd;
_savedGroupDescriptors.Add(new GroupDescriptor { Member = gd.Member, DisplayContent = gd.DisplayContent, SortDirection = gd.SortDirection });
}
else if (igd is ColumnGroupDescriptor)
{
var cgd = (ColumnGroupDescriptor) igd;
_savedGroupDescriptors.Add(new ColumnGroupDescriptor { Column = cgd.Column, DisplayContent = cgd.DisplayContent, SortDirection = cgd.SortDirection });
}
}
}
break;
case RefreshItemSourceLifetimeStage.FinishedRefresh:
_grid.GroupDescriptors.Clear();
foreach(var groupDescriptor in _savedGroupDescriptors)
{
_grid.GroupDescriptors.Add(groupDescriptor);
}
break;
default:
throw new ArgumentOutOfRangeException("stage");
}
}
Any ideas?
해결책
Funnily enough updating to the latest version of the Telerik controls (2011 Q3 at the time of writing, up from 2011 Q2 SP1) seemed to fix the issue.