2014-11-22 62 views
0

我有一个包含五个字段的表格。我想通过点击一个字段来更新其中的任何一个。但我不知道哪些领域将被修改,这意味着列名不predefined.My代码是在这里如何更新列名称为动态的表格列

public void Edit(int Id, string field, string oldValue, string newValue, object customer) 
{ 

    BanglaFireExt.DAL.ExtDotNetEntities db = new BanglaFireExt.DAL.ExtDotNetEntities(); 

    string message = "<b>Property:</b> {0}<br /><b>Field:</b> {1}<br /><b>Old Value:</b> {2}<br /><b>New Value:</b> {3}"; 
    var obj = db.Employees.Single(x=>x.Id==Id); 
    **obj.field=newValue;**//here field is a string, but there is a column in table named **field value** 
    db.SaveChanges(); 

} 

我如何将字符串转换为表列类型。请帮助我

回答

0

您是否需要设置一个属性作为字段参数传递的名称?你可以使用反射。

obj.GetType().GetProperty(field).SetValue(obj, newValue); 
+0

非常感谢,它完美的作品,我想要什么 – 2014-11-22 19:20:35