DataFormatString not working with date format in GridView

I set the DataFormatString to {0:d} and also tried {0:MM-dd-yyyy} as several forums and articles suggested; but it still wouldn’t work and kept on displaying the entire string including the time. It turns out, all I had to do is add HtmlEncode=”false”.

<asp:GridView ID=”gridItems” runat=”server” AutoGenerateColumns=”False” DataKeyNames=”ItemId”
DataSourceID=”MyItemsDataSource” Width=”100%”>
<asp:BoundField DataField=”Title” HeaderText=”Title” SortExpression=”Title” />
<asp:BoundField DataField=”Status” HeaderText=”Status” SortExpression=”Status” />
<asp:BoundField DataField=”DateCreated” HeaderText=”Listed on” SortExpression=”DateCreated”
DataFormatString=”{0:d}” HtmlEncode=”false” />


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s