在WPF System.Windows.Data中使用DataTable和DataGrid时出现错误

发布于 2024-10-03 08:08:41 字数 1652 浏览 0 评论 0原文

我使用WPFToolkit的DataGrid来显示一些数据。

DataTable 在 myfile.xaml.cs 中初始化,

myTable = new DataTable();
DataColumn col;
col = new DataColumn();
col.DataType = System.Type.GetType("System.Int64");
col.ColumnName = "ID";
col.ReadOnly = true;
col.Unique = false;
myTable.Columns.Add(col);

col = new DataColumn();
col.DataType = System.Type.GetType("System.String");
col.ColumnName = "Name";
col.ReadOnly = true;
col.Unique = false;
myTable.Columns.Add(col);

依此类推。

正如此处建议的,我

myGrid.ItemsSource = myTable.DefaultView;

在 myfile.xaml.cs 中使用。

在 myfile.xaml 中,我只定义了

<my:DataGrid Name="myGrid" xmlns:my="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"/>

当我向表中添加条目时,

DataRow row = myTable.NewRow();
row["ID"] = 123;
row["Name"] = "MyName";

Action action = () => myTable.Rows.Add(row);
Dispatcher.Invoke(action);

条目正确添加到 GUI 中的网格,但是我收到以下错误:

System.Windows.Data Error: 39 : BindingExpression path error: 'ID' property not found on 'object' ''Object' (HashCode=29890231)'. BindingExpression:Path=ID; DataItem='Object' (HashCode=29890231); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 39 : BindingExpression path error: 'Name' property not found on 'object' ''Object' (HashCode=29890231)'. BindingExpression:Path=Name; DataItem='Object' (HashCode=29890231); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

那么如何修复此错误? 有什么提示吗?

谢谢。

I use DataGrid of WPFToolkit to display some data.

The DataTable is initialized in myfile.xaml.cs with

myTable = new DataTable();
DataColumn col;
col = new DataColumn();
col.DataType = System.Type.GetType("System.Int64");
col.ColumnName = "ID";
col.ReadOnly = true;
col.Unique = false;
myTable.Columns.Add(col);

col = new DataColumn();
col.DataType = System.Type.GetType("System.String");
col.ColumnName = "Name";
col.ReadOnly = true;
col.Unique = false;
myTable.Columns.Add(col);

and so on.

As suggested here I use

myGrid.ItemsSource = myTable.DefaultView;

in myfile.xaml.cs.

In myfile.xaml I have only defined

<my:DataGrid Name="myGrid" xmlns:my="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"/>

When I add an entry to the table with

DataRow row = myTable.NewRow();
row["ID"] = 123;
row["Name"] = "MyName";

Action action = () => myTable.Rows.Add(row);
Dispatcher.Invoke(action);

the entry is added correctly to the grid in the GUI, however I receive the following error:

System.Windows.Data Error: 39 : BindingExpression path error: 'ID' property not found on 'object' ''Object' (HashCode=29890231)'. BindingExpression:Path=ID; DataItem='Object' (HashCode=29890231); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 39 : BindingExpression path error: 'Name' property not found on 'object' ''Object' (HashCode=29890231)'. BindingExpression:Path=Name; DataItem='Object' (HashCode=29890231); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

So how can I fix this error?
Any hints?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼波传意 2024-10-10 08:08:41

我认为这与您执行代码的顺序有关。尝试将其更改

DataRow row = myTable.NewRow();
row["ID"] = 123;
row["Name"] = "MyName";

Action action = () => myTable.Rows.Add(row);
Dispatcher.Invoke(action);

为:

DataRow row = myTable.NewRow();
myTable.Rows.Add(row);
row["ID"] = 123;
row["Name"] = "MyName";

即:在将项目添加到行之前将行添加到数据表中

I think this has something to do with the order in which you execute your code. Try changing this:

DataRow row = myTable.NewRow();
row["ID"] = 123;
row["Name"] = "MyName";

Action action = () => myTable.Rows.Add(row);
Dispatcher.Invoke(action);

into this:

DataRow row = myTable.NewRow();
myTable.Rows.Add(row);
row["ID"] = 123;
row["Name"] = "MyName";

i.e. : add your row to the dataTable BEFORE you add the items to the row

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文