469,609 Members | 1,124 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

DataFormatString not working on a GridView DateTime column

I have a GridView that is bound to a collection of DateTimes (i.e. -
Collection<DateTime>). I have a BoundField in the GridView, and its
DataField is set to the DateTime's Date property, and the DataFormatString
is set to "{0:d}" in order to display the date in short date format (without
the time). But the DataFormatString doesn't seem to have any effect. The
DateTime gets displayed with the time.

I have another GridView that is bound to a collection of business objects,
and the business objects have a DateTime property. One of the columns in
the GridView has a BoundField that uses this DateTime property as its
DataField. In this case, the "{0:d}" DataFormatString works as it should.
(The DateTime is displayed in short date format, without the time.) So it
works in this case, but not when I have the GridView bound to a collection
of DateTimes.

I've worked around the problem by using the RowDataBound event to manually
trim the time from the end of the cell's text, but that's a kludgy
workaround. Does anyone know why the DataFormatString isn't working
correctly in this case?

Thanks in advance,
--Rob Roberts
Apr 9 '06 #1
2 16074
Hi,

set HtmlEncode="False" to the BoundField.

Reason is that by default data is HTML-encoded before string formatting
takes place (which actually prevents string formatting to apply at all)
For an explanation see:
http://aspadvice.com/blogs/joteke/ar.../25/12871.aspx

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

"Rob Roberts" <ro************@AndThisToo.pcisys.net> wrote in message
news:uV****************@TK2MSFTNGP04.phx.gbl...
I have a GridView that is bound to a collection of DateTimes (i.e. -
Collection<DateTime>). I have a BoundField in the GridView, and its
DataField is set to the DateTime's Date property, and the DataFormatString
is set to "{0:d}" in order to display the date in short date format
(without the time). But the DataFormatString doesn't seem to have any
effect. The DateTime gets displayed with the time.

I have another GridView that is bound to a collection of business objects,
and the business objects have a DateTime property. One of the columns in
the GridView has a BoundField that uses this DateTime property as its
DataField. In this case, the "{0:d}" DataFormatString works as it should.
(The DateTime is displayed in short date format, without the time.) So it
works in this case, but not when I have the GridView bound to a collection
of DateTimes.

I've worked around the problem by using the RowDataBound event to manually
trim the time from the end of the cell's text, but that's a kludgy
workaround. Does anyone know why the DataFormatString isn't working
correctly in this case?

Thanks in advance,
--Rob Roberts

Apr 9 '06 #2
"Teemu Keiski" <jo****@aspalliance.com> wrote in message
news:OH**************@TK2MSFTNGP03.phx.gbl...
Hi,

set HtmlEncode="False" to the BoundField.


Yes, that fixed it. Thank you!

--Rob Roberts
Apr 9 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Hardy Wang | last post: by
1 post views Thread by Neven Klofutar | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.