使用反射获取列名称

时间:2014-11-05 12:04:05

标签: 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.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();}

有人能指出我正确的方向吗?

0 个答案:

没有答案