Serialcoder en Français Serialcoder in English
TEL : +33 (0)9 72 13 15 17

Windows Forms FAQ resources

5. Windows Forms Datagrid

5.44 How do I format a date column in a datagrid?


If you have added a table style to your datagrid (so individual column styles have been generated), then you can use code such as this to set the Format property of the particular column style.

[C#]
     //add format col 3 columnstyle where column 3 holds a date...

     DataGridTextBoxColumn dgtbc;
     dgtbc = dataGrid1.TableStyles[0].GridColumnStyles[3] as DataGridTextBoxColumn;

     if(dgtbc != null)
           dgtbc.Format = "g"; // or "u" or whatever format you want to see

[VB.NET]
     'add format col 3 columnstyle where column 3 holds a date...

     Dim dgtbc as DataGridTextBoxColumn
     dgtbc = CType(dataGrid1.TableStyles(0).GridColumnStyles(3), DataGridTextBoxColumn)

     If Not dgtbc is Nothing Then
           dgtbc.Format = "g" ' or "u" or whatever format you want to see
     End If