C# DataGridView。选择EdItem[0]可选择GenericType
首先,我将DataGridView的datacontext分配给一个匿名类型,该匿名类型是从泛型类C# DataGridView。选择EdItem[0]可选择GenericType,c#,wpf,datagridview,anonymous-types,C#,Wpf,Datagridview,Anonymous Types,首先,我将DataGridView的datacontext分配给一个匿名类型,该匿名类型是从泛型类Company中获得的,最好使用匿名类型来获得要在DataGridView中显示的所需列名 var companyData = (from c in dataContext.Companies select new { Company =c.CompanyName, City=c.Ci
Company
中获得的,最好使用匿名类型来获得要在DataGridView中显示的所需列名
var companyData = (from c in dataContext.Companies
select new
{
Company =c.CompanyName,
City=c.City.CityName,
});
dataGridView.DataContext = companyData;
现在我想在MouseDoubleClick事件发生时获取select行的值。但问题是我无法将匿名类型转换回我的泛型类型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);
提前感谢。使用扩展方法将DataGridViewRow转换为任何类型
public static class DataGridViewRowWExtenstions
{
public static T GetObject<T>(this DataGridViewRow Row) where T : new()
{
List<PropertyInfo> properties = typeof(T).GetProperties().ToList();
return CreateItemFromRow<T>(Row, properties);
}
private static T CreateItemFromRow<T>(DataGridViewRow row, List<PropertyInfo> properties) where T : new()
{
T item = new T();
foreach (var property in properties)
{
if (row.DataGridView.Columns.Contains(property.Name))
{
if (row.Cells[property.Name] != null)
property.SetValue(item, row.Cells[property.Name].Value, null);
}
}
return item;
}
}
private void dataGridView2_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
DataGridViewRow selectedRow = dataGridView2.SelectedRows[0];
Company company = selectedRow.GetObject<Company>();
}
公共静态类DataGridViewRowWextensions
{
公共静态T GetObject(此DataGridViewRow行),其中T:new()
{
List properties=typeof(T).GetProperties().ToList();
返回CreateItemFromRow(行,属性);
}
私有静态T CreateItemFromRow(DataGridViewRow行,列表属性),其中T:new()
{
T项=新的T();
foreach(属性中的var属性)
{
if(row.DataGridView.Columns.Contains(property.Name))
{
if(row.Cells[property.Name]!=null)
property.SetValue(项,行。单元格[property.Name]。值,null);
}
}
退货项目;
}
}
私有void dataGridView2_CellMouseDoubleClick(对象发送方,DataGridViewCellMouseEventArgs e)
{
DataGridViewRow selectedRow=dataGridView2.SelectedRows[0];
公司=selectedRow.GetObject();
}
使用扩展方法将DataGridViewRow转换为任何类型
public static class DataGridViewRowWExtenstions
{
public static T GetObject<T>(this DataGridViewRow Row) where T : new()
{
List<PropertyInfo> properties = typeof(T).GetProperties().ToList();
return CreateItemFromRow<T>(Row, properties);
}
private static T CreateItemFromRow<T>(DataGridViewRow row, List<PropertyInfo> properties) where T : new()
{
T item = new T();
foreach (var property in properties)
{
if (row.DataGridView.Columns.Contains(property.Name))
{
if (row.Cells[property.Name] != null)
property.SetValue(item, row.Cells[property.Name].Value, null);
}
}
return item;
}
}
private void dataGridView2_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
DataGridViewRow selectedRow = dataGridView2.SelectedRows[0];
Company company = selectedRow.GetObject<Company>();
}
公共静态类DataGridViewRowWextensions
{
公共静态T GetObject(此DataGridViewRow行),其中T:new()
{
List properties=typeof(T).GetProperties().ToList();
返回CreateItemFromRow(行,属性);
}
私有静态T CreateItemFromRow(DataGridViewRow行,列表属性),其中T:new()
{
T项=新的T();
foreach(属性中的var属性)
{
if(row.DataGridView.Columns.Contains(property.Name))
{
if(row.Cells[property.Name]!=null)
property.SetValue(项,行。单元格[property.Name]。值,null);
}
}
退货项目;
}
}
私有void dataGridView2_CellMouseDoubleClick(对象发送方,DataGridViewCellMouseEventArgs e)
{
DataGridViewRow selectedRow=dataGridView2.SelectedRows[0];
公司=selectedRow.GetObject();
}
但是我没有得到显示所需的列名。我已经提到了我使用匿名类型的目的,我不认为有任何更干净的方法可以做到这一点,尽管有一些黑客。您应该创建一个具体的类(用于命名),并通过LINQ查询而不是匿名类型创建类对象列表为什么需要将对象转换回公司?原始的公司
来自哪里?@phoog:它的SQL表和Linq to SQLClass@Marshal这里的正常方法是创建一个CompanyView类型以显示在dataGridView控件中,而不是使用匿名类型。这样可以更容易地获取对象的属性,并使用它们从数据库中获取正确的公司。但是,我无法获取所需的列名以供显示。我已经提到了我使用匿名类型的目的,我不认为有任何更干净的方法可以做到这一点,尽管有一些黑客。您应该创建一个具体的类(用于命名),并通过LINQ查询而不是匿名类型创建类对象列表为什么需要将对象转换回公司?原始的公司
来自哪里?@phoog:它的SQL表和Linq to SQLClass@Marshal这里的正常方法是创建一个CompanyView类型以显示在dataGridView控件中,而不是使用匿名类型。这样可以更容易地获取对象的属性,并使用它们从数据库中获取正确的公司。