Reputation: 7030
In my DataGridView, I am assigning my values to cells like this:
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;
...
Pretty dirty stuff. The cell names match the properties of my List of pointCommonInfo and pointSpecificInfo, so I decided to do use reflection:
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 = //?
}
}
I can get the names of the field, but I don't know how to actually access them using reflection. Any guidance would be appreciated.
Upvotes: 1
Views: 1075
Reputation: 1474
If you say - cell names match to the properties of pointCommonInfo and pointSpecificInfo - then you should use GetProperties() instead of GetFields(). You can achieve it as following:
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;
}
Upvotes: 2
Reputation: 3358
Just use FieldInfo.GetValue
Method: 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]);
}
Upvotes: 1