【问题标题】:Using DisplayNameAttribute in DevExpress PivotGridControl在 DevExpress PivotGridControl 中使用 DisplayNameAttribute
【发布时间】:2019-01-18 19:48:39
【问题描述】:

有没有办法使用为每个属性定义的 [DisplayName] 作为 PivotGridControl 中的字段?

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

使用以下代码导致将 IdNum 显示为 Pivot Fields,但我希望显示 IdentifireNumber > 改为:

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

【问题讨论】:

    标签: c# winforms devexpress displayname-attribute pivot-grid


    【解决方案1】:

    您可以将显示名称分配给fieldCaption 属性。

    例如:

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

    或者你可以在pivotGridControl.Fields 上循环,并为每个PivotGridField 设置它的标题:

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

    【讨论】:

      【解决方案2】:

      应该正确应用 DisplayName 属性。您的代码在最新版本 18.1 中为我正常运行。或者,考虑改用Data Annotation Attributes。将DisplayAttribute 应用于必填字段。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-10
        • 1970-01-01
        相关资源
        最近更新 更多