我正在为我的 wpf 应用程序使用 telrik RadGridView。我想将网格绑定到我在 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 ---