RadGridView数据绑定

时间:2013-02-10 05:51:14

标签: wpf data-binding telerik radgridview

我正在使用telrik RadGridView为我的wpf应用程序。我想将网格绑定到SQL数据库中的表。我认为这很简单,但我没有太多运气。我正在获取SQL数据并将其存储在正确填充的DataSet中。我在屏幕上唯一得到的是没有数据填充的表格列。我的RadGridView的XAML是:

 <telerik:RadGridView Name="Grid" Grid.Row="2" Grid.ColumnSpan="3"  HorizontalAlignment="Center" VerticalAlignment="Bottom" 
                         AlternateRowBackground="AliceBlue" SelectionMode="Multiple" 
                         AutoGenerateColumns="False" MinHeight="300" MinWidth="800"  CanUserResizeColumns="True" CanUserResizeRows="True"
                         FilteringMode="FilterRow"  IsFilteringAllowed="True"  CanUserSortColumns="True" GridLinesVisibility="Both" 
                         DataLoadMode="Asynchronous"  >
        <telerik:StyleManager.Theme>
            <telerik:Windows8Theme/>
        </telerik:StyleManager.Theme>
        <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumn Width="40" Header="Add" DataMemberBinding="{Binding Add}" IsGroupable="False" IsFilterable="True" >
</telerik:GridViewDataColumn>
            <telerik:GridViewDataColumn Width="75" Header="Qty"  IsGroupable="False" IsFilterable="True">
</telerik:GridViewDataColumn>
            <telerik:GridViewDataColumn Width="75" Header="ID" DataMemberBinding="{Binding ID}" IsGroupable="False" IsFilterable="True"/>
            <telerik:GridViewDataColumn Width="200" Header="Description" DataMemberBinding="{Binding Description}" IsGroupable="False" IsFilterable="True"/>
            <telerik:GridViewDataColumn Width="75" Header="Price" DataMemberBinding="{Binding Price}" IsGroupable="False" IsFilterable="True"/>
            <telerik:GridViewDataColumn Width="75" Header="Min" DataMemberBinding="{Binding Min}" IsGroupable="False" IsFilterable="True"/>
        </telerik:RadGridView.Columns>

    </telerik:RadGridView>

在cs代码中,我正在填充数据集并使用

将datacontext设置为网格
      Grid.DataContext = ds.Tables[0].DefaultView;         

我还是WPF编程的新手,所以我不确定我是否遗漏了一些未成熟的东西,或者我正在尝试做一些根本无法做到的事情。谢谢你们给予的任何帮助。

编辑:这是加载网格时立即窗口的一些输出。

A first chance exception of type 'System.ArgumentException' occurred in System.Data.dll A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll System.Windows.Data Error: 17 : Cannot get 'Item[]' value (type 'Object') from '' (type 'DataRowView'). BindingExpression:Path=[]; DataItem='DataRowView' (HashCode=30892613); target element is 'ValueSetter' (Name=''); target property is 'Value' (type 'Object') TargetInvocationException:'System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: is neither a DataColumn nor a DataRelation for table Catalog. at System.Data.DataRowView.get_Item(String property) --- End of inner exception stack trace --- at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture) at MS.Internal.Data.PropertyPathWorker.GetValue(Object item, Int32 level) at MS.Internal.Data.PropertyPathWorker.RawValue(Int32 k)' A first chance exception of type 'System.ArgumentException' occurred in System.ComponentModel.DataAnnotations.dll A first chance exception of type 'System.ArgumentException' occurred in System.Data.dll A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll System.Windows.Data Error: 17 : Cannot get 'Item[]' value (type 'Object') from '' (type 'DataRowView'). BindingExpression:Path=[]; DataItem='DataRowView' (HashCode=37343064); target element is 'ValueSetter' (Name=''); target property is 'Value' (type 'Object') TargetInvocationException:'System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: is neither a DataColumn nor a DataRelation for table Catalog. at System.Data.DataRowView.get_Item(String property) --- End of inner exception stack trace --- at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture) at MS.Internal.Data.PropertyPathWorker.GetValue(Object item, Int32 level) at MS.Internal.Data.PropertyPathWorker.RawValue(Int32 k)' A first chance exception of type 'System.ArgumentException' occurred in System.ComponentModel.DataAnnotations.dll A first chance exception of type 'System.ArgumentException' occurred in System.Data.dll A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll System.Windows.Data Error: 17 : Cannot get 'Item[]' value (type 'Object') from '' (type 'DataRowView'). BindingExpression:Path=[]; DataItem='DataRowView' (HashCode=17870819); target element is 'ValueSetter' (Name=''); target property is 'Value' (type 'Object') TargetInvocationException:'System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: is neither a DataColumn nor a DataRelation for table Catalog. at System.Data.DataRowView.get_Item(String property) --- End of inner exception stack trace ---

3 个答案:

答案 0 :(得分:1)

在与Telerik客户支持部门谈了一点之后,问题就在于 DataLoadMode = Asynchronous
不起作用,不得不被取出。此外,自从我使用行虚拟化以来,我需要在网格上设置一个高度。

答案 1 :(得分:0)

而不是
Grid.DataContext = ds.Tables[0].DefaultView;

这样做 Grid.ItemsSource = ds.Tables[0].DefaultView;

我的意思是它应该是网格的ItemsSource属性,而不是DataContext。我不确定这一点,但我想是的。

答案 2 :(得分:-1)

为我工作:

RadGridView.ValidatesOnDataErrors = "InEditMode"

另见http://www.telerik.com/forums/argumentexception-when-using-icustomtypedescriptor