我是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.Nullable
1[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;
问题是当我需要添加新列/更改数据网格列位置时,我需要更改整个分配值的索引。为了解决这个问题,我想用上面提到的方式用列名赋值。