Posts
208
Comments
1144
Trackbacks
51
ASP.NET 2.0 GridView FormatStrings

You can supply format strings to the columns in your GridView by setting the DataFormatString property of the column to something like this:  “{0:d}”.  However, you may have run across a case where you set this property and the system does not recognize the format string you provided.  To make this work, you need to set the HtmlEncode property of that column to “False”.  Then the DataFormatString will act as expected when displayed at run-time.

posted on Saturday, February 25, 2006 11:01 AM Print
Comments
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
D'Arcy from Winnipeg
2/25/2006 9:38 PM
Steve, you the man! This was an on my issue list for a current project...couldn't figure out why my Price column wasn't recognizing my currency format string. Thanks for blogging this!
:)

D
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
THANK YOU!!!
3/16/2006 1:04 PM
I seriously looked around for 2 hours trying to get the gridview to format as currency...not a single other person mentioned this little detail...thanks you very much
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
Gavin
3/22/2006 1:00 PM
<asp:boundcolumn datafield="Price" headertext="ex. VAT" dataformatstring="&euro;{0:F2}">

or

<asp:boundcolumn datafield="Price" headertext="Tax" dataformatstring="{0:c}">
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
bfs
3/24/2006 6:22 AM
Nice Tip! Thanks!
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
John Abbott
6/23/2006 9:27 PM
Fortunately this was the first article I found when I searched Google. I had already spent a lot of time searching Microsoft's site and the help files.

Thanks for the tip!
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
Pedro Vera
7/13/2006 7:07 PM
This is the second time that stupid htmlencode property screws with me. You probably saved me another hour of pulling my hair getting the stupid currency formats working!

Thanks so much!!!
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
rb
10/19/2006 2:18 PM
Thank you, thank you!

I had spend the morning on this. I was scratching my head...well, I have {0:d} in there so what the heck is wrong...
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
Julian Hardy
10/27/2006 9:40 AM
I have to add my thanks in here too... This had two of us newbies banging our heads on a wall trying to figure this one out!

Cheers!
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
Doug Kelley
11/1/2006 11:15 AM
Thank you so much! I have to agree with the others, this had me stumped for quite awhile until I found your tip. What amazes me is that Microsoft does not mention the HtmlEncode property dependancy in the help files!
Gravatar
# Thank you!
j
11/20/2006 5:43 PM
Another "thank you" from someone who just couldn't figure out why it wasn't working right.
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
Nodar
2/14/2007 4:16 AM
Many thanks! )
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
louis
2/22/2007 4:16 PM
tank goodness!
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
Hasan
3/13/2007 5:47 AM
Oh thanks for the tip man! I was looking why my grid won't render my text according the format string!
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
donkey
7/17/2007 2:01 AM
thanks man!
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
Gul
7/29/2007 2:39 AM
Thanks for the great information,

I have a TemplateField and in ItemTemplate I have written Eval("Price", "{0:c}"), but it is not applying and I don't find HtmlEncode Propery for TemplateField.
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
Ryan
8/7/2007 8:58 AM
Thanks and gig em!
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
klrfz1
8/17/2007 9:53 AM
<ItemTemplate>
<%# Server.HtmlEncode( Eval("Price", "{0:c}").ToString() ) %>
</ItemTemplate>


Might work.
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
Shankar
9/10/2007 7:38 AM
Huh! I spent few hours make it to work. But I landed here - Thanks
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
Carlito
10/30/2007 12:30 AM
God bless you Steve for those wonderful four sentences of handy information!!!!!!!!!!!!!!
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
Michael
5/19/2008 3:59 PM
I tried the same thing, but it didn't work. Any idea what's going wrong?
thanks!
Case "Currency"
With bf
.HtmlEncode = False
.DataFormatString = "{0:c}"
End With
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
Steve
5/19/2008 4:20 PM
Where does this CASE statement live? Is it in the page load event? Have you tried putting the HtmlEncode="False" directly in the mark up of your *.aspx page?
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
Michael
5/30/2008 10:33 AM
The case is in a subroutine. Yes, it's all called from page load, but I'm trying to avoid putting code directly into the *.aspx page! I'd like this code to be reusable.
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
Jay
6/17/2008 5:39 AM
Cool! It worked, thanks Steve!.
one question, what dataformatstring should i use to make the field display Php 5,000.00?
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
mama
7/9/2008 4:48 PM
How can i format a file size to KB within a gridview?.

What i do is using the DirectoryInfo and list files in a gridview. so just wanted to format the file size to be displayed in KB.
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
sallie
7/14/2008 12:58 PM
I displayed the file size using a label, and here's how I got KBs (in the Text attribute):

<asp:Label id="Label2" runat="server" Text='<%# FormatNumber(Eval("Length")/1000, 0) %>'
Gravatar
#  ASP.NET 2.0 GridView FormatStrings
ahmed
7/17/2008 6:17 AM
how to load data in gridview & delete, update date in gridview

Thanks
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
ahmed
7/17/2008 6:18 AM
how to load data in gridview, edit, delete, update data in gridview asp.net c# 2.0

Thanks
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
Sue
11/8/2008 10:15 AM
I can format just fine but when I try to Update the row in the SQL Server database it is apparantly attempting to send the formatted string and I get a database error. Can you display in currency format but on update revert to the original money datatype?
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
Paulo
5/26/2009 12:17 PM
sallie: you should divide for 1024,0 not for 1000,0
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
manjunath
10/9/2009 9:15 AM
Thank You
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
anugro
11/30/2009 1:42 AM
Steve, Thanks.. u the man for this problem
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
desperate housewife
1/7/2010 10:23 AM
Thanks man, this is the solution, this is it!
Gravatar
# re: ASP.NET 2.0 GridView FormatStrings
Vishal
10/2/2011 11:28 PM
thanks for this post. As this has save lots of my time.

Post Comment

Title *
Name *
Email
Comment *  
Verification

View Steve Michelotti's profile on LinkedIn

profile for Steve Michelotti at Stack Overflow, Q&A for professional and enthusiast programmers




Google My Blog

Tag Cloud