How do I format a date column in a datagrid

Platform: WinForms| Category: 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

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.