How to format a date field in the SPGridView control

Ever use the SPGridView control and have the need to format a date column? It’s actually pretty simple.

 

BoundField field1 = new BoundField();field1.HeaderText = “Date Field”;field1.DataField = “DateField”;field1.HtmlEncode = true;field1.DataFormatString = @”{0:M/dd/yyyy}”;_ResultsGridView.Columns.Add(field1);

The key is to set HtmlEncode to true and set the DataFormatString with the appropriate date format.

 

SharePoint Web Services

About the SharePoint !New icon