In order to change the DateTime format of an entire column or a specific cell, you must set the column's/cell's FormatProvider property to the desired format and the FormatSpecifier property to the appropriate format provider.
By default, the DateTime format will be the system's format. Therefore, if the system short date is yyyy/mm/dd, if you set the FormatSpecifier property to "d", without setting the FormatProvider property, the result will be a short date with the system format.
If you want to change the way in which short dates are displayed (or any other date format), then you will need to create an instance of the DateTimeFormatInfo class, change its ShortDatePattern property to the desired short date format and assign the DateTimeFormatInfo object to the column's/cell's FormatProvider property.
Demonstration
To change a column's DateTime format, you can use the following code:
VB.NET |
Copy Code |
Dim format As New System.Globalization.DateTimeFormatInfo() format.ShortDatePattern = "yyyy/mm/dd"
GridControl1.Columns( "OrderDate" ).FormatSpecifier = "d" GridControl1.Columns( "OrderDate" ).FormatProvider = format
|
C# |
Copy Code |
System.Globalization.DateTimeFormatInfo format = new System.Globalization.DateTimeFormatInfo(); format.ShortDatePattern = "yyyy/mm/dd";
gridControl1.Columns[ "OrderDate" ].FormatSpecifier = "d"; gridControl1.Columns[ "OrderDate" ].FormatProvider = format;
|
If you want to change the DateTime format for a specific cell, you can set the cell's FormatProvider and FormatSpecifier properties rather than the column's:
VB.NET |
Copy Code |
Dim format As New System.Globalization.DateTimeFormatInfo() format.ShortDatePattern = "yyyy/mm/dd"
GridControl1.DataRows( 0 ).Cells( "OrderDate" ).FormatSpecifier = "d" GridControl1.DataRows( 0 ).Cells( "OrderDate" ).FormatProvider = format
|
C# |
Copy Code |
System.Globalization.DateTimeFormatInfo format = new System.Globalization.DateTimeFormatInfo(); format.ShortDatePattern = "yyyy/mm/dd";
gridControl1.DataRows[ 0 ].Cells[ "OrderDate" ].FormatSpecifier = "d"; gridControl1.DataRows[ 0 ].Cells[ "OrderDate" ].FormatProvider = format;
|
Of course, if you create an instance of a column or cell, you can also set its FormatSpecifier and FormatProvider properties.
VB.NET |
Copy Code |
Dim format As New System.Globalization.DateTimeFormatInfo() format.ShortDatePattern = "yyyy/mm/dd"
Dim column As New Column( "MyColumn", GetType( String ) ) column.FormatSpecifier = "d" column.FormatProvider = format
|
C# |
Copy Code |
System.Globalization.DateTimeFormatInfo format = new System.Globalization.DateTimeFormatInfo(); format.ShortDatePattern = "yyyy/mm/dd";
Column column = new Column( "MyColumn", typeof( string ) ); column.FormatSpecifier = "d"; column.FormatProvider = format;
|