【发布时间】:2019-05-24 18:02:58
【问题描述】:
我想获取所选单元格的值作为字符串。但我不能选择它作为实体框架对象。
我尝试使用不是来自实体框架的对象列表来完成它,它工作正常:
public MainWindow()
{
InitializeComponent();
using (SellEntities Context = new SellEntities())
{
var query = from t in Context.Categories
orderby t.Id
select new
{
t.Id,
CategoryName = t.Name
};
CategoriesDataGridVeiw.ItemsSource = query.ToList();
}
}
private void CategoriesDataGridVeiw_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var rows = CategoriesDataGridVeiw.SelectedItem as Category;
if (rows != null)
{
MessageBox.Show(rows.Name);
}
else
MessageBox.Show("Null");
CategoriesDataGridVeiw.UnselectAll();
}
【问题讨论】:
-
这需要minimal reproducible example。不清楚你在问什么。
-
问题是这个变量(行)总是=空。我无法从数据网格中选择 Item 作为类别或任何由 Entity Framework 制作的类。我试图澄清我的问题,但我不知道如何描述它。
-
嗯,很明显
new { t.Id, CategoryName = t.Name }不能转换为Category。但我不明白问题描述。 我不能选择它作为实体框架对象 -- 为什么不呢? -
非常感谢。你让我跟踪我的代码找到解决方案。我已经被这个问题困扰了 4 天。
标签: c# wpf entity-framework datagrid