2010-08-24 216 views
1

我正在使用PivotGrid(DevExpress)。我想在for循環中設置AppearancePrint屬性設置。動態設置屬性值

在下面的例子中,我如何在Cell中使用變量類型?

所以不是

grid.AppearancePrint.Cell.BackColor = Color.White; 
grid.AppearancePrint.Cell.BackColor2 = Color.LightBlue; 

我想這樣做:

//datarow example <PrintAppearance Type="Cell" Font="Tahoma,8,Regular" BackColor="White" BackColor2="Light Grey"/> 

foreach (DataRow dr in appearances)   
{ 
    string type = dr["Type"].ToString(); 
    grid.AppearancePrint.[type].BackColor = Color.FromName(dr["BackColor"].ToString()); 
    grid.AppearancePrint.[type].BackColor2 = Color.FromName(dr["BackColor2"].ToString()); 
} 

回答

2

這實質上是一種腳本解析的形式,您需要使用反射才能做到這一點。例如:

foreach (DataRow dr in appearances) { 
    string type = dr["Type"].ToString(); 

    PropertyInfo propertyForType = grid.AppearancePrint.GetType().GetProperty(type); 
    object objectForProperty = propertyForType.GetValue(grid.AppearancePrint, null); 

    PropertyInfo propertyForBackColor = objectForProperty.GetType().GetProperty("BackColor"); 
    PropertyInfo propertyForBackColor2 = objectForProperty.GetType().GetProperty("BackColor2"); 

    propertyForBackColor.SetValue(objectForProperty, Color.FromName(dr["BackColor"].ToString()), null); 
    propertyForBackColor2.SetValue(objectForProperty, Color.FromName(dr["BackColor2"].ToString()), null); 
} 
+0

謝謝。它做到了。 – 2010-08-24 20:01:59

0

我不熟悉您的具體問題,但一眼,似乎你需要使用反射因爲直到運行時纔會知道該類型 - 如果您不熟悉反射,它將允許您檢查對象(更重要的是它的屬性)

請參閱here可能的解決方案

+0

感謝您的鏈接。我打算試試這個 Type type = target.GetType(); PropertyInfo prop = type.GetProperty(「propertyName」); prop.SetValue(target,propertyValue,null); prop.SetValue(target,propertyValue,null); 同時,我找到了另一種方法來做到這一點,而不使用反射 DevExpress.Utils.AppearanceObject ao = grid.AppearancePrint.GetAppearance(type); ao.Options.UseFont = true; ao.BackColor = Color.FromName(dr [「BackColor」]。ToString()); Registers HS – 2010-08-24 20:06:03