C# 使用反射获取列名
我正在winForm应用程序中使用实体模型从datagridview插入数据。这是我的代码:C# 使用反射获取列名,c#,entity-framework,reflection,datagridview,C#,Entity Framework,Reflection,Datagridview,我正在winForm应用程序中使用实体模型从datagridview插入数据。这是我的代码: using (var context = new EmployeeEntities()) { EMPLOYEE emp= new EMPLOYEE (); foreach (DataGridViewRow row in dgvLoadTable.Rows) { emp.EMP_ID = (row.Cells[0].Value == null) ? (int?)null : int.Pa
using (var context = new EmployeeEntities())
{
EMPLOYEE emp= new EMPLOYEE ();
foreach (DataGridViewRow row in dgvLoadTable.Rows)
{
emp.EMP_ID = (row.Cells[0].Value == null) ? (int?)null : int.Parse(row.Cells[0].Value.ToString());
emp.DEPARTMENT = (row.Cells[1].Value == null) ? string.Empty : row.Cells[1].Value.ToString();
context.EMPLOYEE.AddObject(emp);
context.SaveChanges();
}
}
现在我想使用反射更改列名。我试过这个,但不起作用:
EMPLOYEE emp = new EMPLOYEE();
foreach (DataGridViewRow row in dgvLoadTable.Rows)
{
foreach (DataGridViewColumn column in dgvLoadTable.Columns)
{
column.HeaderText = column.Index.ToString();
emp.GetType().GetProperty(column.HeaderText).GetValue(entityName, null) = (row.column.Value == null) ? (int?)null : int.Parse(row.column.Value.ToString()); ;
}
entityName.EMPLOYEE.AddObject(emp);
entityName.SaveChanges();}
有人能给我指出正确的方向吗?你到底想做什么??DB中的表有自己的列名,对吗?然后你在做什么
column.HeaderText=column.Index.ToString()代码>你想从中获得什么?主要是,我只想得到表的列名。在这里,我试图从datagridview列标题中获取列名。您的意思是不想为每个列更正分隔行吗?像emp.emp\u ID=values
并想为所有列做一行概括??获取标题名和对应的名称,设置实体的属性并保存它?或者您只想获取它的列名或任何其他原因。。你说你想得到列名??但代码似乎是在设置列名。。你到底想要什么。。获取或设置??是的,我想为所有列创建一个通用行。