首先,我将 DataGridView 的 datacontext 分配给从 Generic Class 获得的Company
匿名类型 首选匿名类型以获得要在 DataGridView 中显示的所需列名。
var companyData = (from c in dataContext.Companies
select new
{
Company =c.CompanyName,
City=c.City.CityName,
});
dataGridView.DataContext = companyData;
现在我想在 MouseDoubleClick 事件时获取选择行值。但问题是我无法将匿名类型转换回我的泛型类型 Company。
void dataGridView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var selectedRow = dataGridView.SelectedItem[0];
// How to convert selectedRow back to Company ?
// Anonymous type have no implementation of AsEnumerable.
}
我想要这样的东西:
Company company = selectedRow.Select(c=>new Company
(CompanyName=selectedRow.Company,
CityName=selectedRow.City);
先感谢您。