Data binding 当Xceed DataGridControl绑定到DataTable且排序列不再存在时引发异常

Data binding 当Xceed DataGridControl绑定到DataTable且排序列不再存在时引发异常,data-binding,xceed-datagrid,Data Binding,Xceed Datagrid,我试图使用Xceed()提供的扩展WPF工具包社区版中的优秀DataGrid。我有一个应用程序,它在一个简单的DataGridControl对象中显示报告的结果。用户可以从报告列表中选择报告,数据网格使用与报告关联的数据表动态更新。每个报表的DataTable中的列在名称和数量上都可能有所不同。对于WPF中的默认控件,使用常规MVVM数据绑定可以很好地工作。这也适用于Xceed的DataGridControl,除非使用列对数据进行排序或分组。 当一个列被排序或分组,并且DataTable被更新为

我试图使用Xceed()提供的扩展WPF工具包社区版中的优秀DataGrid。我有一个应用程序,它在一个简单的DataGridControl对象中显示报告的结果。用户可以从报告列表中选择报告,数据网格使用与报告关联的数据表动态更新。每个报表的DataTable中的列在名称和数量上都可能有所不同。对于WPF中的默认控件,使用常规MVVM数据绑定可以很好地工作。这也适用于Xceed的DataGridControl,除非使用列对数据进行排序或分组。

当一个列被排序或分组,并且DataTable被更新为一个没有该列的列时,DataGridControl抛出ArgumentException,表示被排序的列不存在。以下是一个例外示例:

System.ArgumentException未处理
消息=“”类型没有名为“SAP\u MATERIAL\u NUMBER”的属性,因此无法对数据进行排序 收藏。
Source=PresentationFramework堆栈跟踪: 位于System.Windows.Data.BindingListCollectionView.ConvertSortDescriptionCollection(SortDescriptionCollection 排序) 位于System.Windows.Data.BindingListCollectionView.RefreshOverride()处 在System.Windows.Data.CollectionView.Refresh()中 在System.Windows.Data.CollectionView.EndDefer()中 在System.Windows.Data.CollectionView.DeferHelper.Dispose()中 位于System.Windows.Controls.ItemCollection.SetCollectionView(CollectionView (视图) 位于System.Windows.Controls.ItemCollection.SetItemsSource(IEnumerable 价值) 在System.Windows.Controls.ItemsControl.OnItemsSourceChanged(DependencyObject)中 d、 DependencyPropertyChangedEventArgs(附件e)

下面是我当前定义和绑定控件的XAML:

<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>

是否有人以这种方式使用此数据网格,并找到了解决此问题的方法?

我想我找到了我的问题,或者至少找到了一种在不引发异常的情况下处理此问题的方法。我通过XAML代码修改,为我的网格使用显式DataGridCollectionViewSource声明:

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

然后更新我的DataGridControl,将其用作ItemsSource,而不是直接绑定到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>


一旦我这样做了,如果排序列或分组列不存在,它就不再抛出异常,数据网格也会按预期更新。

我想我找到了问题所在,或者至少找到了一种方法来处理这个问题,而不会抛出异常。我通过XAML代码修改,为我的网格使用显式DataGridCollectionViewSource声明:

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

然后更新我的DataGridControl,将其用作ItemsSource,而不是直接绑定到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>

一旦我这样做了,如果排序列或分组列不存在,并且数据网格按预期更新,它就不再抛出异常