Exception thrown when Xceed DataGridControl is bound to a DataTable and a sorted column no longer exists

StackOverflow https://stackoverflow.com/questions/15101496

  •  15-03-2022
  •  | 
  •  

質問

I am trying to use the excellent DataGrid available in the Extended WPF Toolkit Community Edition made available by Xceed (http://wpftoolkit.codeplex.com/). I have an application that displays the results from reports in a simple DataGridControl object. The user can select the report from a list of reports and the data grid dynamically updates using a DataTable associated to the report. The columns in each Report's DataTable can vary both in name and quantity. With the default controls in WPF this works just fine using regular MVVM data binding. This also works fine with the DataGridControl from Xceed, except for when a column has been used to sort or group the data.

What happens is when a column is sorted or grouped, and the DataTable is updated to one that doesn't have the column in it, the DataGridControl throws an ArgumentException saying the column being sorted doesn't exist. Here's an example exception:

System.ArgumentException was unhandled
Message='' type does not have property named 'SAP_MATERIAL_NUMBER', so cannot sort data collection.
Source=PresentationFramework StackTrace: at System.Windows.Data.BindingListCollectionView.ConvertSortDescriptionCollection(SortDescriptionCollection sorts) at System.Windows.Data.BindingListCollectionView.RefreshOverride() at System.Windows.Data.CollectionView.Refresh() at System.Windows.Data.CollectionView.EndDefer() at System.Windows.Data.CollectionView.DeferHelper.Dispose() at System.Windows.Controls.ItemCollection.SetCollectionView(CollectionView view) at System.Windows.Controls.ItemCollection.SetItemsSource(IEnumerable value) at System.Windows.Controls.ItemsControl.OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) ...

Here's my current XAML that defines and binds the control:

<xcdg:DataGridControl
    Grid.Row="2"
    AutoCreateColumns="True"
    AutoRemoveColumnsAndDetailConfigurations="True"
    ReadOnly="True"
    x:Name="xceedReportResult"
    ItemsSource="{Binding SelectedReport.Report.Result}"
    FontSize="11">

    <xcdg:DataGridControl.View>
        <xcdg:TableflowView
            ShowRowSelectorPane="False"
            IsAnimatedColumnReorderingEnabled="True"
            HorizontalGridLineBrush="LightGray"
            VerticalGridLineBrush="LightGray"
            IsAlternatingRowStyleEnabled="True"
            ShowScrollTip="False">

            <xcdg:TableflowView.Theme>
                <xcdg:ClassicSystemColorTheme />
            </xcdg:TableflowView.Theme>
        </xcdg:TableflowView>
    </xcdg:DataGridControl.View>

</xcdg:DataGridControl>

...following some advice from the Xceed forums I've tried running the following code when a new report is selected in hopes of clearing out any SortDescriptions or GroupDescriptions, but this isn't working right:

ICollectionView source = xceedReportResult.ItemsSource as DataGridCollectionView;

if (source != null)
{
    if (source.SortDescriptions != null)
    {
        source.SortDescriptions.Clear();
    }

    if (source.GroupDescriptions != null)
    {
        source.GroupDescriptions.Clear();
    }
}

Has anyone used this data grid in this way, and found a way around this issue?

役に立ちましたか?

解決

I think I found my issue, or at least a way to handle this without having exceptions be thrown. I modified by XAML code to use an explicit DataGridCollectionViewSource declaration for my grid:

<Control.Resources>
   <xcdg:DataGridCollectionViewSource
        x:Key="reportResultView"
        x:Name="reportResultView"
        Source="{Binding SelectedReport.Report.Result.DefaultView}"
        AutoCreateItemProperties="True"/>
</Control.Resources>

and then update my DataGridControl to use this as the ItemsSource instead of binding directly to the DataTable:

<xcdg:DataGridControl
    Grid.Row="2"
    AutoCreateColumns="True"
    AutoRemoveColumnsAndDetailConfigurations="True"
    ReadOnly="True"
    x:Name="xceedReportResult"
    ItemsSource="{Binding Source={StaticResource reportResultView}}"
    FontSize="11">

    <xcdg:DataGridControl.View>
        <xcdg:TableflowView
            ShowRowSelectorPane="False"
            IsAnimatedColumnReorderingEnabled="True"
            HorizontalGridLineBrush="LightGray"
            VerticalGridLineBrush="LightGray"
            IsAlternatingRowStyleEnabled="True"
            ShowScrollTip="False">

            <xcdg:TableflowView.Theme>
                <xcdg:ClassicSystemColorTheme />
            </xcdg:TableflowView.Theme>
        </xcdg:TableflowView>
    </xcdg:DataGridControl.View>

</xcdg:DataGridControl>

Once I do that it no longer throws exceptions if the sorted or grouped column(s) don't exist, and the data grid updates as expected.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top