【问题标题】:C# Changing background color of an CustomFieldData eventC# 更改 CustomFieldData 事件的背景颜色
【发布时间】:2011-12-28 15:00:35
【问题描述】:

我有一个工作正常的 PivotDataGrid。还添加了一个CustomUnboundFieldData,但现在我想根据该字段中的值更改单元格的背景颜色。

要更改颜色,我使用 customCellAppearance 事件。只有在我操作未绑定字段数据中的值后才会触发此事件。

所以我的问题基本上是,如何更改单元格的背景。使用未绑定的字段数据事件?

在代码的 sn-p 下方

//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; 
    }

}      

【问题讨论】:

  • 你的哪一行代码负责改变颜色?
  • 函数“Control_CustomCellAppearance”对此负责。我刚刚添加了这个功能。

标签: c# wpf devexpress


【解决方案1】:

要根据“原始数据”改变颜色,请使用 CreateDrillDownDataSource 方法。

通过这个方法,你可以得到源列,并根据方法输出的值改变单元格的背景颜色。

在代码sn-p下面:

    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

【讨论】:

    猜你喜欢
    • 2011-12-18
    • 2015-10-02
    • 1970-01-01
    • 2011-06-26
    • 2013-08-08
    • 1970-01-01
    相关资源
    最近更新 更多