c#devexpress xtragrid列格式

c#devexpress xtragrid列格式,c#,devexpress,xtragrid,C#,Devexpress,Xtragrid,我正在尝试设置XtraGrid组件中列的自定义格式: public class Customers { public object A { get; set; } } Customers c61=新客户(); c61.A=DateTime.Now.AddDays(1); 客户c62=新客户(); c62.A=DateTime.Now.AddDays(3); List tmpList=新列表(); tmpList.Add(c61); tmpList.Add(c61); gridCont

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

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

Customers c61=新客户();
c61.A=DateTime.Now.AddDays(1);
客户c62=新客户();
c62.A=DateTime.Now.AddDays(3);
List tmpList=新列表();
tmpList.Add(c61);
tmpList.Add(c61);
gridControl1.DataSource=tmpList;
GridColumn gc=新的GridColumn();
(gridControl1.MainView作为GridView.Columns.AddRange(新的DevExpress.XtraGrid.Columns.GridColumn[]{gc});
gc.Caption=“A”;
gc.FieldName=“A”;
(gridControl1.MainView作为GridView)。列[“A”]。DisplayFormat.FormatType=DevExpress.Utils.FormatType.DateTime;
(gridControl1.MainView作为GridView)。列[“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";
Customers c61=新客户();
c61.A=DateTime.Now.AddDays(1);
客户c62=新客户();
c62.A=DateTime.Now.AddDays(3);
List tmpList=新列表();
tmpList.Add(c61);
tmpList.Add(c61);
gridControl1.DataSource=tmpList;
(gridControl1.MainView作为GridView)。列[“A”]。DisplayFormat.FormatType=DevExpress.Utils.FormatType.DateTime;
(gridControl1.MainView作为GridView)。列[“A”]。DisplayFormat.FormatString=“dd/MM/yyyy”;

在您的第一个示例中,有自动填充的可见列和使用自定义格式显式创建的隐藏列。当您显式添加列时,默认情况下列是隐藏的。您需要通过将
GridColumn.Visible
属性设置为
true
或使用其
GridColumn.VisibleIndex
属性来显示它。另外,当您显式添加列时,最好使用
GridView.options行为.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";
Customers c61=新客户();
c61.A=DateTime.Now.AddDays(1);
客户c62=新客户();
c62.A=DateTime.Now.AddDays(3);
List tmpList=新列表();
tmpList.Add(c61);
tmpList.Add(c61);
var view=gridControl1.MainView作为GridView;
view.OptionsBehavior.AutoPopulateColumns=false//
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";