如何从数据表中设置数据网格控件值?
我使用此来源
public static readonly DependencyProperty ObjDataTableDefaultViewProperty = DependencyProperty.Register("ObjDataTableDefaultView", typeof(System.Data.DataView), typeof(Window1), new FrameworkPropertyMetadata());
public System.Data.DataView ObjDataTableDefaultView
{
get { return (System.Data.DataView)GetValue(ObjDataTableDefaultViewProperty); }
set { SetValue(ObjDataTableDefaultViewProperty, value); }
}
private void CreateObjDataTable()
{
try
{
ObjDataTableDefaultView = table.DefaultView;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
但是它收到了错误消息并且程序停止了,
类型的第一次机会异常 ' System.Windows.Markup.XamlParseException'发生在 PresentationFramework.dll
其他信息:无法创建' Window1'定义于 assembly' ReadSky,Version = 1.0.0.0,Culture = neutral, 公钥=空&#39 ;.一个人的目标抛出了例外 调用。标记文件错误' Window1.xaml'第1行第9位。
My XAML
<Window x:Class="ReadSky.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="296" Width="738" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit">
<Grid>
<my:DataGrid AutoGenerateColumns="False" Margin="36,30,39,90" Name="gridCtrl" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" IsReadOnly="True" RowBackground="Gray" AlternatingRowBackground="White" ItemsSource="{Binding ObjDataTableDefaultView, ElementName=uc, Mode=OneWay}"/>
</Grid>
</Window>
答案 0 :(得分:1)
你可以试试这个:
<DataGrid Name="grid" AutoGenerateColumns="True" ItemsSource="{Binding}" />
public DataTable TableData {
get {
DataTable dt = new DataTable();
dt.Columns.Add("col1");
dt.Columns.Add("col2");
dt.Rows.Add(new string[] {"val1", "val2"});
return dt;
}
}
grid.DataContext = TableData;