Search code examples
c#winformsdevexpressdisplayname-attributepivot-grid

Using DisplayNameAttribute in DevExpress PivotGridControl


Is there any way to use [DisplayName] defined for each property as fields in a PivotGridControl?

public class Dto
{
   [DisplayName("Identifier")]
   public int Id {get;set;}
   [DisplayName("Number")]
   public string Num {get;set;}
   ...
   //other properties 
}

using following code cause show Id and Num as Pivot Fields, but I expect show Identifire and Number instead:

IList<Dto> result = CreateDtoList();
var bsOrderItems = new BindingSource(DataSource = result);
pivotGridControl.DataSource = bsOrderItems;
pivotGridControl.RetrieveFields();

Solution

  • You can assign display names to Caption property of the field.

    For example:

    //using System.ComponentModel;
    var properties = TypeDescriptor.GetProperties(typeof(Dto));
    foreach (PropertyDescriptor p in properties)
        pivotGridControl.Fields[p.Name].Caption = p.DisplayName;
    

    Or you can loop on the pivotGridControl.Fields and for each PivotGridField set it's caption:

    //using System.ComponentModel;
    //using DevExpress.XtraPivotGrid;
    var properties = TypeDescriptor.GetProperties(typeof(Dto));
    foreach (PivotGridField f in pivotGridControl.Fields)
        f.Caption = properties[f.FieldName].DisplayName;