【发布时间】:2016-07-22 04:21:16
【问题描述】:
我是 WPF 的初学者。我正在尝试使用 DataRow/DataRowView 读取 Datagrid Selected Items。我的代码如下 -
foreach (System.Data.DataRowView row in dgDocument.SelectedItems)
{
txtPhotoIDNo.Text = row["PhotoIdNumber"].ToString();
}
但我面临以下错误--
“无法转换类型的对象 'f__AnonymousTypeb
11[System.String,System.Byte,System.String,System.String,System.String,System.Byte[],System.Nullable1[System.DateTime],System.String,System.Nullable`1[System.DateTime],System.String,System.String]' 键入“System.Data.DataRowView”。”
当我尝试使用以下方式时,效果很好-
(dgDocument.SelectedCells[2].Column.GetCellContent(item) as TextBlock).Text;
问题是当我需要添加新列/更改数据网格列位置时,我需要更改整个分配值的索引。为了解决这个问题,我想用上面提到的方式用列名赋值。
【问题讨论】:
-
DataGrid控件与System.Data/DataTable等无关。SelectedItems属性包含您添加到网格中的实际对象(在这种情况下似乎是匿名类型)。您应该研究数据绑定 - 您不应该尝试像这样直接设置文本。 -
拜托!举个例子吧。
标签: c# wpf datagrid datarow datarowview