1

在我的 DataGridView 中,我将我的值分配给这样的单元格:

for (int i = 0; i < count; i++)
{
    int j = i + 1;
    DGVPointCtrl.Rows.Add(new DataGridViewRow());

    DGVPointCtrl.Rows[j].Cells["pointidentifier"].Value = pointCommonInfo[i].pointidentifier;
    DGVPointCtrl.Rows[j].Cells["pointname"].Value = pointCommonInfo[i].pointname;
    DGVPointCtrl.Rows[j].Cells["backup"].Value = pointCommonInfo[i].backup;
    DGVPointCtrl.Rows[j].Cells["covenable"].Value = pointCommonInfo[i].covenable;
    DGVPointCtrl.Rows[j].Cells["covlifetime"].Value = pointCommonInfo[i].covlifetime;
    DGVPointCtrl.Rows[j].Cells["covtarget"].Value = pointCommonInfo[i].covtarget;
    DGVPointCtrl.Rows[j].Cells["description"].Value = pointCommonInfo[i].description;
    DGVPointCtrl.Rows[j].Cells["historyenable"].Value = pointCommonInfo[i].historyenable;
    DGVPointCtrl.Rows[j].Cells["pointaddress"].Value = pointCommonInfo[i].pointaddress;
    DGVPointCtrl.Rows[j].Cells["pointtype"].Value = pointCommonInfo[i].pointtype;
    DGVPointCtrl.Rows[j].Cells["activetext"].Value = pointSpecificInfo[i].activetext;
    DGVPointCtrl.Rows[j].Cells["alarmenable"].Value = pointSpecificInfo[i].alarmenable;

    DGVPointCtrl.Rows[i].Cells["alarmenablehigh"].Value = pointSpecificInfo[i].alarmenablehigh;
    DGVPointCtrl.Rows[i].Cells["alarmenablelow"].Value = pointSpecificInfo[i].alarmenablelow;
    DGVPointCtrl.Rows[j].Cells["alarmvalue"].Value = pointSpecificInfo[i].alarmvalue;
    DGVPointCtrl.Rows[j].Cells["correctvalue"].Value = pointSpecificInfo[i].correctvalue;
    DGVPointCtrl.Rows[j].Cells["covincrement"].Value = pointSpecificInfo[i].covincrement;
    ...

很脏的东西。单元格名称与我的 pointCommonInfo 和 pointSpecificInfo 列表的属性相匹配,因此我决定使用反射:

for (int i = 0; i < count; i++)
{
    int j = i + 1;
    DGVPointCtrl.Rows.Add(new DataGridViewRow());
    FieldInfo[] fieldCommon = typeof(PointCommonInformation).GetFields();
    FieldInfo[] fieldSpecific = typeof(PointSpecificInformation).GetFields();
    foreach (FieldInfo field in fieldCommon)
    {
        DGVPointCtrl.Rows[j].Cells[field.Name].Value = //?
    }

    foreach (FieldInfo field in fieldSpecific)
    {
        DGVPointCtrl.Rows[j].Cells[field.Name].Value = //?
    }   
}

我可以获得该字段的名称,但我不知道如何使用反射来实际访问它们。任何指导将不胜感激。

4

2 回答 2

2

如果您说 - 单元名称与 pointCommonInfo 和 pointSpecificInfo 的属性匹配 - 那么您应该使用 GetProperties() 而不是 GetFields()。您可以通过以下方式实现它:

Type commonType = typeof(PointCommonInformation);

foreach (PropertyInfo item in commonType.GetProperties())
{
        object propertyObject = item.GetValue(pointCommonInfo, null);
        string propertyValue = propertyObject == null ? string.Empty : propertyObject.ToString();
        DGVPointCtrl.Rows[j].Cells[item.Name].Value = propertyValue;    
}
于 2012-09-17T04:25:42.793 回答
1

只需使用FieldInfo.GetValue方法:http: //msdn.microsoft.com/en-us/library/system.reflection.fieldinfo.getvalue.aspx

foreach (FieldInfo field in fieldCommon)
{
    DGVPointCtrl.Rows[j].Cells[field.Name].Value = field.GetValue(pointCommonInfo[i]);
}

foreach (FieldInfo field in fieldSpecific)
{
    DGVPointCtrl.Rows[j].Cells[field.Name].Value = field.GetValue(pointCommonInfo[i]);
}  
于 2012-09-17T04:18:36.730 回答