Welcome to Professional ASP.NET - Chris Love's Official Blog Sign in | Join | Help

Chris Love's Official ASP.NET Blog

Chris Love's Helpful tips, tricks and pragmatic development knowledge for the ASP.NET world.
Add to Technorati Favorites


ASP Insider Follow Me On Twitter
Gridview DataFormatString not working

I have really been struggling lately with the Gridview.  Most of the time I like to use a label, literal or placeholder in a template column so this does not really affect me.  But I still use a BoundColumn for some fields, like a date or price type thing.  In DataGrids I always was able to use DataFormatString="{0:c} for a currency format and DataFormatString="{0:d}" for a date.

To my surprise this did not work in the GridView.  After a little research I found the GridView has a new BoundField property, HTMLEncode.  You must set HTMLEncode=false to get your DataFormatString expression to be applied to your data.  I did this and voila, property formatting.

Posted: Wednesday, April 19, 2006 4:32 PM

by Chris Love
Filed under: ,

Comments

will said:

I can't seem to get this to work in visual studio 2008. Is anyone else experiencing this issue?
# June 11, 2008 6:58 PM

pfatcat said:

JOY!!!! thanks very much
# August 8, 2008 3:35 PM

Nivedhitha said:

Thank u ,its working.
# August 9, 2008 1:50 AM

Jimmy C said:

How exactly do you set HTMLEncode to false when adding the GridView & Bound Columns dynamically?
# October 16, 2008 1:27 PM

sangam said:

Thank you a lot. I was also facing the same problem. Not it is solved.
# November 11, 2008 6:13 AM

Mikez said:

This feature does not work as described in 2008.
# November 17, 2008 6:40 PM

Steve A said:

Nice one...got my problem sorted quick time. Thanks
# December 13, 2008 4:37 PM

Eric said:

Having the same problem in VS 2008
# January 28, 2009 11:20 AM

ayesha said:

but it is also not working in {0:N2}
# June 30, 2009 4:20 AM

Ram Mohan said:

DataFormatstring in not working in VS2008
# August 26, 2009 2:58 AM

George said:

gridview is adding time to a date string that does not have it. The soultion presented here does not work with VS2008.
# June 11, 2010 5:40 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS