Reputation: 459
I am trying to set custom format of column within XtraGrid component:
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";
It is not working. But when I simple remove adding column explicitly, XtraGrid automatically populates columns and format is working properly. Working code:
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";
Upvotes: 0
Views: 1475
Reputation: 6631
In your first example there is autopopulated visible column and explicitly created hidden column with custom format. When you adding column explicitly then your column is hidden by default. You need to show it by setting the GridColumn.Visible
property to true
or using its GridColumn.VisibleIndex
property. Also, when you adding column explicitly is better to turn of the autopopulation of grid view by using the GridView.OptionsBehavior.AutoPopulateColumns
property.
Here is example:
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";
Upvotes: 1