I’m using Matt Dotson’s Real World GridView in an ASP.NET 4 page, connecting to MS SQL Server 2005. Everything works as it should except for the DataFormatString property on a field with the datetime type – I’m trying to get just the shortdate (dd/MM/yyyy) to show up but it never changes away from long time format (dd/MM/yyyy hh:mm:ss tt).
Here’s what I’ve tried:
- Setting the HtmlEncode property to False
- Removing the Culture and UICulture from my page
- Both of the above with different DataFormatStrings – {0:dd/MM/yyyy} and {0:d} (The latter because of this document)
Any ideas what I might have missed?
tl;dr:
Set the ApplyFormatInEditMode property to “True”.
Okay, this should have been obvious – it seems the DataFormatString only applies when the row isn’t in edit mode. I changed the ReadOnly property to “true” on that field and the DataFormatString was applied. Of course, this doesn’t fix the problem when the users are editing a pre-existing form since all the loaded rows will be in edit mode. Then I noticed the “ApplyFormatInEditMode” property. It did exactly what it says.
This applies to normal GridViews as well, so I’ll edit the title of the question.