BoundField DataFormatString attribute not being applied. - Raj Kaimal

BoundField DataFormatString attribute not being applied.

Version: ASP.net 2.0 RTM

I wasted a few minutes figuring out this one.

You have a BoundField object in a GridView bound to a field of type DateTime with a DataFormatString attribute but the format string is not being applied.

<asp:BoundField DataField="DateOfBirth" DataFormatString="{0:MM/dd/yyyy}" />

Instead, the field appears to be formatted using its ToString() method like so:
Output: 10/31/2005 7:00:54 PM
 

Cause

To prevent cross site scripting attacks, the field value is HtmlEncoded. The HtmlEncoding occurs before applying the DataFormatString making the format string have no effect.

 

Resolution

In this case (ie. when using a field of type DateTime), set HtmlEncode to false.

<asp:BoundField DataField="DateOfBirth" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="false"/>

The Output renders as: 10/31/2005

This setting also applies to the HeaderText.
The following:
HeaderText="Employee<BR/>Name"
will not render a line break unless you set HtmlEncode to false.


I am not sure why the ASP.net team decided against HtmlEncoding the string after applying the DataFormatString.

Update for 3.5 SP1, 3.0 SP1, 2.0 SP1
SP1 introduces a new property called HtmlEncodeFormatString which allows you to specify whether the formatted text should be HTML encoded when it is displayed.

Published Monday, October 31, 2005 9:08 PM by rajbk
Filed under:

Comments

# re: BoundField DataFormatString attribute not being applied.

Tuesday, November 01, 2005 2:30 AM by Hannes Preishuber

# re: BoundField DataFormatString attribute not being applied.

That really looks like a bug to me. I can't think of any reason why the HtmlEncoding should happen before applying the FormatString. You should report it to LadyBug.

Tuesday, November 01, 2005 6:37 AM by Rob Cannon

# re: BoundField DataFormatString attribute not being applied.

This has gotta be one of those "By Design" dumb mistakes that nobody wants to take responsibility for.

That's just retarded. Now I have to go through ALL my gridview columns (at least 20 gridviews with roughly 2-5 columns).

Thanks for wasting my time Microsoft.

Wednesday, November 02, 2005 12:17 PM by Eric Newton

# re: BoundField DataFormatString attribute not being applied.

Thank you! I just spent the last 30 minutes pulling out my hair trying to figure out what happened to my grid formatting. I also agree that this should be classified as a bug more-so than a security feature.

Wednesday, November 02, 2005 3:26 PM by Jim F

# re: BoundField DataFormatString attribute not being applied.

Yikes! This has been driving me crazy for a couple of weeks now. Thrilled I found this, so I can stop doing silly formatting in the database...

Friday, November 11, 2005 5:11 PM by Douglas Reilly

# re: BoundField DataFormatString attribute not being applied.

Hmm... another piece of .net stupidity. Thanks for posting this. I was ready to start tearing my hair out.

Monday, November 14, 2005 1:55 AM by Shane

# re: BoundField DataFormatString attribute not being applied.

Thank you!!!
Our customers attaced us a lot for this.

Tuesday, November 15, 2005 8:34 AM by Audun

# re: BoundField DataFormatString attribute not being applied.

Thank you for saving me even one more minute of me trying to fix that ridiculous issue.

Wednesday, November 16, 2005 3:08 PM by Todd Kitta

# re: BoundField DataFormatString attribute not being applied.

Thank you very much!

I can't believe how many of these little foibles I keep finding in .NET 2.0...

Monday, November 21, 2005 10:51 AM by Lee

# re: BoundField DataFormatString attribute not being applied.

Awesome! Thanks!

Tuesday, November 22, 2005 2:45 PM by Scott Helm

# re: BoundField DataFormatString attribute not being applied.

This definitely saved me a lot of headaches. Just like to mention for Google's benefit that this also works for values stored as type MONEY in SQL Server 2005 Express.

Friday, November 25, 2005 4:30 PM by Christopher Nehren

# re: BoundField DataFormatString attribute not being applied.

My thanks also, although I don't share the venom at Microsoft as so many of the other posters. I did spend too much time tracking this down and it's definitely frustrating -- but the ease of use of so many other aspects of asp.net development help my keep my temper in check.

Many thanks, Raj!

Sunday, November 27, 2005 3:02 PM by Chris Munger

# re: BoundField DataFormatString attribute not being applied.

lol, thank God Google found this page.

Tuesday, November 29, 2005 10:23 PM by Jeff Schroeder

# BoundField DataFormatString attribute not working

I was stuck with a problem a few weeks ago where my DataFormatString on a BoundField in an GridView was...

Monday, July 03, 2006 9:14 AM by Steven Smith

# re: BoundField DataFormatString attribute not being applied.

greattttt!

Tuesday, August 08, 2006 12:57 PM by cesar

# re: BoundField DataFormatString attribute not being applied.

Thank you thank you. This was just in time.

Wednesday, August 09, 2006 2:46 AM by Sleepy

# re: BoundField DataFormatString attribute not being applied.

Thanx mate. Thanks for posting this. Dunno how long i would be pulling my hair if not for this post.

Monday, August 21, 2006 4:24 AM by gkeeara

# re: BoundField DataFormatString attribute not being applied.

Thanks! I was starting to think I was crazy...

Tuesday, August 22, 2006 9:29 AM by Marty

# re: BoundField DataFormatString attribute not being applied.

this is a bug, alright... what i did, as a workaround, is to convert my BoundField to a TemplateField but it requires additional codes that's just unnecessary (if the boundfield formatting works correctly). Thanks for the tip!

Tuesday, August 22, 2006 11:40 PM by Leon Tayson

# re: BoundField DataFormatString attribute not being applied.

Thanks, that works perfectly. Definitely sounds like a bug, have you reported it yet? Too bad so many people spit venom at Microsoft for stuff like this... .NET 2.0 works like a charm 95% of the time. More than forgivable that 5% of the time you're going insane to find stuff like this.

Wednesday, August 23, 2006 7:51 AM by Dom

# re: BoundField DataFormatString attribute not being applied.

Thanks from me too! I was also pulling my hair out on this one!!

Wednesday, August 23, 2006 12:50 PM by john h

# re: BoundField DataFormatString attribute not being applied.

I was checking some code in an asp.net book, and couldn't figure out what I was doing wrong. I thought at first it was a data typing issue. You have saved me much frustration. Thanks very much.

Thursday, August 24, 2006 8:22 PM by Michael D

# re: BoundField DataFormatString attribute not being applied.

thanks so much for this.

Saturday, September 09, 2006 6:34 AM by Yang

# re: BoundField DataFormatString attribute not being applied.

This one had be going crazy! thanks a lot for the tip! Saved my some precious time for sure.

Friday, September 15, 2006 8:23 AM by Rajeev

# re: BoundField DataFormatString attribute not being applied.

Thanks! your a ledgend! That problem was really anoying me

Sunday, September 17, 2006 3:28 AM by Andrew

# re: BoundField DataFormatString attribute not being applied.

After reading the documentation for AutoGeneratedField.DataFormatString, I didn't expect the property to work. Even worse, I had no expectation of being able to format a date without much pain. Here is the quote: "Note: This version of the property has been implemented to always return an empty string to indicate that no special formatting is applied to the field values. It is not possible to set this property." The IDE I use (Visual Web Developer 2005 Express Edition) doesn't prevent setting the format string. Hence one expects the string to have some kind of effect. Finding your workaround was SO good. Thanks a lot for saving many hours for all of us.

Wednesday, September 27, 2006 3:29 AM by Peter Christensen

# re: BoundField DataFormatString attribute not being applied.

Well done. Now why hasn't MS at least given a "Behavior by design" notation?

Tuesday, October 24, 2006 2:16 AM by Jim

# re: BoundField DataFormatString attribute not being applied.

This has been driving me crazy, having to make every column templated just to format it.

Thanks!

Tuesday, October 24, 2006 10:11 AM by Joel

# re: BoundField DataFormatString attribute not being applied.

This fixed the problem with the date bu still doesn't work for email. I tried to use this

mailto:{0} and it shows the word mailto. Have no idea how to fix it!

Wednesday, October 25, 2006 10:50 AM by Eleonora

# re: BoundField DataFormatString attribute not being applied.

I'd already converted my field to a template, you're a GENIUS!

Friday, October 27, 2006 8:36 AM by Samson Alogaga

# re: BoundField DataFormatString attribute not being applied.

Ditto the thanks (first google hit!) and ditto the .NET 2.0 has much more good than evil.

Tuesday, October 31, 2006 10:33 PM by Mike Jansen

# re: BoundField DataFormatString attribute not being applied.

You are lucky, you wasted ONLY "a few minutes figuring out this one". Thanks for posting the solution on the net.

Thursday, November 09, 2006 3:43 PM by ASh

# re: BoundField DataFormatString attribute not being applied.

I wasted almost 2 hours researcheg this issue. Thanks for posting the solution.

Tuesday, November 14, 2006 3:54 PM by Gopi

# re: BoundField DataFormatString attribute not being applied.

Thanks a lot for that solution :]

Sunday, November 19, 2006 2:07 PM by Ron Heimann

# re: BoundField DataFormatString attribute not being applied.

Thanks very much, would have taken ages to find this otherwise.

Friday, November 24, 2006 7:54 AM by Ralphy

# re: BoundField DataFormatString attribute not being applied.

Thanks! Saved me a lot of time and headaches.

Thursday, November 30, 2006 7:01 PM by Farhad Tasharofi

# re: BoundField DataFormatString attribute not being applied.

Gracias por aliviar al menos un par de horas en investigación!!!!

Saludos

Jaime

Tuesday, December 05, 2006 2:31 PM by Jaime

# re: BoundField DataFormatString attribute not being applied.

This also applies to formatting a numeric (int) field to be shown as a currency.

<asp:BoundField DataField="PRECIO EUROS" DataFormatString="{0:#,#.##} €" HeaderText="PRECIO"                          SortExpression="PRECIO" HtmlEncode="false" />

Output: 240.000 €

Wednesday, December 06, 2006 2:54 AM by virginiadisweb

# re: BoundField DataFormatString attribute not being applied.

Yeah, ta mate.

Saved my sanity!

Thursday, December 14, 2006 3:55 PM by dave

# re: BoundField DataFormatString attribute not being applied.

Thanks also, and to all the folks who are posting up yet more "features"... Mike K.

Tuesday, December 19, 2006 5:55 AM by Mike Kingscott

# re: BoundField DataFormatString attribute not being applied.

thanks so much for this.

Wednesday, January 10, 2007 5:39 AM by zuber patel

# re: BoundField DataFormatString attribute not being applied.

Top ranked item on google for "DataFormatString", and it works! thanks Raj, you've been most helpful

Tuesday, January 16, 2007 9:45 PM by Beric Holt

# re: BoundField DataFormatString attribute not being applied.

Thanks so much, I wasted a lot of hours of my live with this stuff, the worst of all is that in every tutorial that I see any one say something of this

Thursday, January 25, 2007 10:39 PM by Ram n Ontiveros

# re: BoundField DataFormatString attribute not being applied.

Awesome! Works perfect!

Wednesday, January 31, 2007 4:19 PM by Adam

# re: BoundField DataFormatString attribute not being applied.

Just wanted to add my thanks to the many...

Tuesday, February 06, 2007 1:38 PM by doug

# re: BoundField DataFormatString attribute not being applied.

Thanks! I'm glad Google found this page.

Monday, February 12, 2007 10:19 AM by John

# re: BoundField DataFormatString attribute not being applied.

I must be extremely lucky - I beGoogled this issue within mere minutes of realizing the DataFormatString wasn't working, and found this lovely tip.  Now I'm wondering if I don't want to just apply HtmlEncode="false" to the entire page, to spare the hassle of setting it on every column.

Thanks, Raj!  You're the king.

Tuesday, February 13, 2007 10:57 AM by skaught

# re: BoundField DataFormatString attribute not being applied.

What did people do before Google?

Wednesday, February 21, 2007 7:10 AM by Jon

# re: BoundField DataFormatString attribute not being applied.

Thanks, You da Man

Sunday, March 11, 2007 12:20 AM by Allnight Coder

# Programmer&#8217;s Blog &raquo; Blog Archive &raquo; Using DataFormatString attribute with BoundField objects

# How the date will be formatted in gridview bound column as "24-Apr-2007"

I am new to asp.net ..Ur reply will helps me a lot...

Wednesday, April 25, 2007 9:24 AM by ASP.NET Spider

# re: BoundField DataFormatString attribute not being applied.

Spider,

{0:dd-MMM-yyyy}

Reference: http://msdn2.microsoft.com/en-us/library/8kb3ddd4(VS.71).aspx

Wednesday, April 25, 2007 9:44 AM by rajbk

# re: BoundField DataFormatString attribute not being applied.

Thanks... I was about to tear my hair out over this one!

Monday, April 30, 2007 4:28 AM by Hardeep Virdee

# re: BoundField DataFormatString attribute not being applied.

That's really great! Appreciate your post saved me so much time.

Friday, May 04, 2007 2:29 PM by hm

# re: BoundField DataFormatString attribute not being applied.

well, solution seems perfect except that htmlEncode property doesn't exist in .net v1.1 datagrid control..

got any idea??

Thursday, June 21, 2007 4:17 AM by poLen

# re: BoundField DataFormatString attribute not being applied.

That really works ! Thanks you for the help.

Friday, June 22, 2007 5:25 PM by Jeff Jones

# re: BoundField DataFormatString attribute not being applied.

Thank u very much..........

Thursday, July 12, 2007 5:59 AM by Kumar M

# re: BoundField DataFormatString attribute not being applied.

Thanks! I had the same issue, but luckily I found your post here almost immediately.

Saturday, July 14, 2007 8:03 PM by Brent

# re: BoundField DataFormatString attribute not being applied.

Thanks very much for this ... luckily I only was at it for 10 minutes before I found this post (and looking at the dates of comments, it looks as though I'm not the only one that find this continually applicable). Kudos to you!

Tuesday, July 24, 2007 8:29 PM by Jarrod

# re: BoundField DataFormatString attribute not being applied.

Thanks much, but is anyone else having trouble with this in a DetailsView? It works in view mode, but when I switch to edit mode, the full date and time shows up in the form field:

7/22/2007 12:00:00 AM

Thursday, July 26, 2007 11:14 AM by Dan Clem

# re: BoundField DataFormatString attribute not being applied.

Thanks a lot !

You saved me a lot of time !

Sunday, July 29, 2007 8:29 PM by Nicolas Brassard

# re: BoundField DataFormatString attribute not being applied.

Hey, thanks for posting your finds.  Google helped me find this page, and you helped me solve this issue!  And what a stupid issue it is...

Monday, July 30, 2007 4:56 PM by Josh Stodola

# re: BoundField DataFormatString attribute not being applied.

Dan Clem...

You need to set the ApplyFormatInEditMode property for the field to true.

Tuesday, August 14, 2007 11:32 AM by Chuck Levy

# re: BoundField DataFormatString attribute not being applied.

Thanks for it, it helped me alot

Tuesday, August 14, 2007 8:17 PM by preet

# re: BoundField DataFormatString attribute not being applied.

Thanks you so much! I spent several hours searching for the answer - finally one of my books (in small print) said DataFormatString won't work unless HTMLEncode is set to false - so at least I knew what to put in Google - then I found your answer. It's a crime that all the 'experts' gloss over this and leave all their demos with 4 zeros after the decimal and the long meaningless date displayed!

Saturday, August 25, 2007 11:53 AM by Gretchen

# re: BoundField DataFormatString attribute not being applied.

Thank God for people like you to answer our problems. I spent quite a lot of time looking for an answer to this. Thank you.

Wednesday, August 29, 2007 10:46 AM by Frosty

# re: BoundField DataFormatString attribute not being applied.

Thank you very much,

Monday, September 03, 2007 2:30 AM by Erkan

# re: BoundField DataFormatString attribute not being applied.

Thanks a lot. It work perfectly.....!

Tuesday, September 11, 2007 12:18 PM by DevEngine

# re: BoundField DataFormatString attribute not being applied.

Thank you very much, you safe me a lot of time wasting for nothing

Sunday, September 16, 2007 7:18 PM by goran_spin

# re: BoundField DataFormatString attribute not being applied.

Not only is this by design, it is documented!!

msdn2.microsoft.com/.../system.web.ui.webcontrols.boundfield.dataformatstring.aspx

"Note:

When the HtmlEncode property is true, the value of the field is HTML encoded to its string representation before the formatting string is applied. For some objects, such as dates, you might want to control how the object is displayed with a formatting string. In those cases, you must set the HtmlEncode property to false."

How horrible can it get... :(

Tuesday, September 18, 2007 9:12 AM by Peter

# re: BoundField DataFormatString attribute not being applied.

Thanks.  This isn't the first time that I ran into this issue.  Hopefully the last.

Thursday, October 04, 2007 3:07 PM by joshblair

# re: BoundField DataFormatString attribute not being applied.

I --was-- a die-hard Microsoft lover.  Well - this one is hard to forgive.   At least they could have put a note in the documentation.  I spent a few hours on it before I decided to ask Google for help..  

Monday, October 08, 2007 4:40 PM by Liz

# re: BoundField DataFormatString attribute not being applied.

hey thanks a lot

Friday, October 26, 2007 11:22 AM by KEETU

# re: BoundField DataFormatString attribute not being applied.

Thanks, it took me a while to figure out, even if I had the MSDN page in front of my eyes :)

Saturday, November 10, 2007 2:54 PM by Mauro

# re: BoundField DataFormatString attribute not being applied.

Why am I the only one this fix isn't working for? :)

<asp:BoundField DataField="EndDate" HeaderText="End Date" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="false" />

Is being rendered as '11/13/2007 12:00:00 AM'

Monday, November 12, 2007 8:11 PM by ScubaChick

# re: BoundField DataFormatString attribute not being applied.

Works like a charm. Great job

Tuesday, November 20, 2007 10:39 AM by ThxRaj

# re: BoundField DataFormatString attribute not being applied.

Thanks for the help!  This was just what I needed to not pull my hair out.  ScubaChick, could the old page be cached?

Saturday, November 24, 2007 11:14 AM by brian

# re: BoundField DataFormatString attribute not being applied.

Thank you for posting! Just what I was looking for.

Tuesday, January 15, 2008 6:26 PM by Jay

# re: BoundField DataFormatString attribute not being applied.

Great!! the same I have been looking for....

Friday, February 01, 2008 2:02 AM by Vipin

# re: BoundField DataFormatString attribute not being applied.

Just to confirm that the page was not cached, I  changed the string to "Mickey Mouse {0:d}" and it showed up as Mickey Mouse 11/13/2007 12:00:00 AM. No formatting. Any ideas?

<asp:BoundField DataField="CreateDate" DataFormatString="Mickey Mouse {0:d}" HtmlEncode="false" HeaderText="Entered" SortExpression="CreateDate" meta:resourcekey="IllustrationsGrid_Header_CreateDate" >

           <itemstyle width="120px"  />

       </asp:BoundField>

Thursday, February 07, 2008 3:35 PM by tom

# re: BoundField DataFormatString attribute not being applied.

Ive got 2 examples, one the datasource comes from a MS Sql db.  Works fine.

2nd comes from an rss feed, and the formating doesnt work.  Im assuming that dot net doesnt know this field data type is a date, and assumes it as a string.

How can i force field "pubDate" into a datetime datatype?

Friday, February 08, 2008 6:59 AM by doof

# re: BoundField DataFormatString attribute not being applied.

Thanks Mate!

You made this cracker happy!

Tuesday, February 12, 2008 11:10 AM by Cracker

# re: BoundField DataFormatString attribute not being applied.

Thanks a lot. Just wasted nearly 40 mins when I should have done a google search from the beginning and found this.

Wednesday, February 13, 2008 10:26 AM by Paul

# re: BoundField DataFormatString attribute not being applied.

htmlencode set to false

does not work when using Gridview in user control .ascx any suggestings???

Thursday, February 28, 2008 10:47 AM by dfour

# re: BoundField DataFormatString attribute not being applied.

Worked great... thanks!

I have a questions... how on earth did you discover the answer to this??

Monday, March 03, 2008 12:38 PM by Bob

# re: BoundField DataFormatString attribute not being applied.

Great, over an hour trying to figure this out.

Tuesday, March 04, 2008 6:04 AM by ML

# re: BoundField DataFormatString attribute not being applied.

Thankyou very much. This post was very helpful to solve my issue on time.

Wednesday, March 05, 2008 9:56 PM by Durga

# re: BoundField DataFormatString attribute not being applied.

Thank you, thank you, thak you!

Thursday, April 03, 2008 10:48 AM by Jon

# re: BoundField DataFormatString attribute not being applied.

great you are life saver...

Friday, July 11, 2008 5:06 PM by jyo

# re: BoundField DataFormatString attribute not being applied.

Cheers,

Saved me time and sanity, chasing this one...

Saturday, September 06, 2008 4:20 AM by Ian Randall

# re: BoundField DataFormatString attribute not being applied.

Thanks, this fix salved me of having the client breathing in mi neck

Friday, December 12, 2008 12:19 PM by dacanetdev

# re: BoundField DataFormatString attribute not being applied.

well, if it is of any consolation, Microsoft made their note regarding the HtmlEncode settig here:

msdn.microsoft.com/.../system.web.ui.webcontrols.boundfield.dataformatstring.aspx

Tuesday, December 23, 2008 3:51 PM by Gridmaniac

# re: BoundField DataFormatString attribute not being applied.

Hi,

can u tell me how to remove the $ symbol which is coming before the values?

Thanks in advance..

Wednesday, May 27, 2009 8:38 AM by sudha

# re: BoundField DataFormatString attribute not being applied.

I was going to cry before read this.

Thanks

Tuesday, October 06, 2009 6:18 AM by Toda

# re: BoundField DataFormatString attribute not being applied.

I felt crazy until I found your Post: very efficient!

Thanks a lot

Monday, October 19, 2009 5:36 AM by Ced69

# re: BoundField DataFormatString attribute not being applied.

The HtmlEncodeFormatString does nothing, at least for me. It's available, and in Intellisense, but I can set it to false or I suppose whatever I want, and still nothing. I'm using 3.5.

The only thing that worked for me was the original solution of HtmlEncode="false".

Monday, March 08, 2010 9:50 PM by Brian

# re: BoundField DataFormatString attribute not being applied.

You saved my time.Thannks a lot!

Thursday, April 22, 2010 12:05 PM by Viji

# re: BoundField DataFormatString attribute not being applied.

Many thanks for this post !

Tuesday, May 18, 2010 4:37 AM by Cedric

# re: BoundField DataFormatString attribute not being applied.

I had this same problem using Dynamic Data, and the htmlEncoding trick did not help.  I did find that in the MetaData class, you have to include the parameter ApplyFormatInEditMode, like this:

<DisplayName("Lockout Start Date*")>

<DisplayFormat(ApplyFormatInEditMode:=True, DataFormatString:="{0:d}")>

<RegularExpression("^(([1-9])|(0[1-9])|(1[0-2]))\/(([0-9])|([0-2][0-9])|(3[0-1]))\/(([0-9][0-9])|([1-2][0,9][0-9][0-9]))$" _

, ErrorMessage:="Must be a valid mm/dd/yyyy format date.")>

Public Property LockoutStartDate() As String

Monday, June 14, 2010 11:19 AM by Charles Young

# re: BoundField DataFormatString attribute not being applied.

Thanks for posting.  This saved me a good deal of time.

Wednesday, June 16, 2010 8:54 AM by Grady Christie

# re: BoundField DataFormatString attribute not being applied.

<asp:BoundField DataField="DateOfBirth" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="false"/>

still The Output renders as: 10/31/2005 7:00:54 PM

:( :( :(

Wednesday, October 06, 2010 2:27 AM by Jahanvi

# re: BoundField DataFormatString attribute not being applied.

Thank for posting. I can untie the rope from the ceiling now.

Friday, October 15, 2010 9:09 AM by Frank

# Dataformat string | Algebratesting

Pingback from  Dataformat string | Algebratesting

Friday, January 06, 2012 1:51 PM by Dataformat string | Algebratesting

Leave a Comment

(required) 
(required) 
(optional)
(required)