2011-12-28 12 views
0

我有一個PivotDataGrid,它工作正常。還添加了一個CustomUnboundFieldData,但現在我想根據此字段中的值更改單元格的背景顏色。C#更改CustomFieldData事件的背景顏色

要更改顏色我使用customCellAppearance事件。在我處理未綁定字段數據中的值後,只有這個事件被觸發。

所以我的問題基本上是,如何改變一個單元格的背景。使用未綁定的字段數據事件?

下面的代碼

//create unbound field 
PivotGridField unboundField = pivot.Control.Fields.Add("unboundDataField", FieldArea.FilterArea); 
unboundField.UnboundType = FieldUnboundColumnType.String; 

//fill unbound field with data 
private void Control_CustomUnboundFieldData(object sender, PivotCustomFieldDataEventArgs e) 
{   

    String myValue = Convert.ToString(e.GetListSourceColumnValue("sourceColumn"));    
    e.Value = myValue.Substring(6); 
    e.Field.SummaryType = FieldSummaryType.Max;    
} 

//code to change appearance of different cells 
private void Control_CustomCellAppearance(object sender, PivotCustomCellAppearanceEventArgs e) 
{  
    if(e.Value != null) 
    { 
     e.Background = System.Windows.Media.Brushes.Green; 
    } 

}  
+0

和你的代碼行負責改變顏色? – 2011-12-28 15:19:32

+0

函數「Control_CustomCellAppearance」負責。我剛剛添加了這個功能。 – Rolf 2011-12-29 08:16:40

回答

0

的一個片段爲了根據「原始數據」改變顏色,使用CreateDrillDownDataSource方法。

使用此方法,您可以獲取源列,並根據來自該方法的值更改單元格的背景顏色。

下面的代碼片段:

private void Control_CustomCellAppearance(object sender, PivotCustomCellAppearanceEventArgs e) 
    { 
     PivotDrillDownDataSource ds = e.CreateDrillDownDataSource(); 
     //get value from the original source according to the row index 
     String myValue = Convert.ToString(ds.GetValue(e.RowIndex, "sourceColumn")); 

     //backgroundcolor condition 
     if(myValue.Containts("something")) 
     { 
      e.Background = System.Windows.Media.Brushes.Green; 
     } 
    } 

欲瞭解更多信息,我指的是DevExpress的網站:http://documentation.devexpress.com/#WindowsForms/DevExpressXtraPivotGridPivotCellBaseEventArgs_CreateDrillDownDataSourcetopic