Using DisplayNameAttribute in DevExpress PivotGridControl

234 Views Asked by At

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();
2

There are 2 best solutions below

0
On BEST ANSWER

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;
0
On

The DisplayName attribute should be applied correctly. Your code operates properly for me in the latest version 18.1. Alternatively, consider using Data Annotation Attributes instead. Apply DisplayAttribute to the required fields.