Home Forums WinForms controls Xceed Grid for WinForms How to change a column’s DateTime format

Viewing 1 post (of 1 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #17806 |

    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.

    VB.NET

    Dim format As New System.Globalization.DateTimeFormatInfo()
    format.ShortDatePattern = “yyyy/mm/dd”

    GridControl1.Columns( “OrderDate” ).FormatSpecifier = “d”
    GridControl1.Columns( “OrderDate” ).FormatProvider = format

    C#

    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

    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#

    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

    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 = form

    C#

    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;

    Imported from legacy forums. Posted by Xceed admin (had 1964 views)

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.