【问题标题】:c# devexpress xtragrid column formatc# devexpress xtragrid 列格式
【发布时间】:2017-03-26 10:50:12
【问题描述】:

我正在尝试在 XtraGrid 组件中设置列的自定义格式:

public class Customers
{
    public object A { get; set; }
}

Customers c61 = new Customers();
c61.A = DateTime.Now.AddDays(1);

Customers c62 = new Customers();
c62.A = DateTime.Now.AddDays(3);

List<Customers> tmpList = new List<Customers>();
tmpList.Add(c61);
tmpList.Add(c61);

gridControl1.DataSource = tmpList;

GridColumn gc = new GridColumn();
(gridControl1.MainView as GridView).Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { gc });

gc.Caption = "A";
gc.FieldName = "A";

(gridControl1.MainView as GridView).Columns["A"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
(gridControl1.MainView as GridView).Columns["A"].DisplayFormat.FormatString = "dd/MM/yyyy";

它不工作。但是当我简单地显式删除添加列时,XtraGrid 会自动填充列并且格式工作正常。工作代码:

Customers c61 = new Customers();
c61.A = DateTime.Now.AddDays(1);

Customers c62 = new Customers();
c62.A = DateTime.Now.AddDays(3);

List<Customers> tmpList = new List<Customers>();
tmpList.Add(c61);
tmpList.Add(c61);

gridControl1.DataSource = tmpList;

(gridControl1.MainView as GridView).Columns["A"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
(gridControl1.MainView as GridView).Columns["A"].DisplayFormat.FormatString = "dd/MM/yyyy";

【问题讨论】:

    标签: c# devexpress xtragrid


    【解决方案1】:

    在您的第一个示例中,有自动填充的可见列和使用自定义格式显式创建的隐藏列。当您显式添加列时,默认情况下您的列是隐藏的。您需要通过将GridColumn.Visible 属性设置为true 或使用其GridColumn.VisibleIndex 属性来显示它。此外,当您显式添加列时,最好使用 GridView.OptionsBehavior.AutoPopulateColumns 属性来关闭网格视图的自动填充。
    这是一个例子:

    Customers c61 = new Customers();
    c61.A = DateTime.Now.AddDays(1);
    
    Customers c62 = new Customers();
    c62.A = DateTime.Now.AddDays(3);
    
    List<Customers> tmpList = new List<Customers>();
    tmpList.Add(c61);
    tmpList.Add(c61);
    
    var view = gridControl1.MainView as GridView;
    view.OptionsBehavior.AutoPopulateColumns = false; // <= Turn off the autopulation before assign the data source.
    
    gridControl1.DataSource = tmpList;
    
    GridColumn gc = new GridColumn();
    
    view.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { gc });
    
    gc.Caption = "A";
    gc.FieldName = "A";
    gc.Visible = true; // <= Unhide your column.
    
    view.Columns["A"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
    view.Columns["A"].DisplayFormat.FormatString = "dd/MM/yyyy";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多