Steve Michelotti

C#, ASP.NET, and other stuff

  Home  |   Contact  |   Syndication    |   Login
  45 Posts | 1 Stories | 133 Comments | 52 Trackbacks

News



Archives

Post Categories

Image Galleries

Articles

Blogs

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

Feedback

# re: ASP.NET 2.0 GridView FormatStrings 2/25/2006 9:38 PM D'Arcy from Winnipeg
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

# re: ASP.NET 2.0 GridView FormatStrings 3/16/2006 1:04 PM THANK YOU!!!
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

# re: ASP.NET 2.0 GridView FormatStrings 3/22/2006 1:00 PM Gavin
<asp:boundcolumn datafield="Price" headertext="ex. VAT" dataformatstring="&euro;{0:F2}">

or

<asp:boundcolumn datafield="Price" headertext="Tax" dataformatstring="{0:c}">


# re: ASP.NET 2.0 GridView FormatStrings 3/24/2006 6:22 AM bfs
Nice Tip! Thanks!

# re: ASP.NET 2.0 GridView FormatStrings 6/23/2006 9:27 PM John Abbott
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!

# re: ASP.NET 2.0 GridView FormatStrings 7/13/2006 7:07 PM Pedro Vera
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!!!

# re: ASP.NET 2.0 GridView FormatStrings 10/19/2006 2:18 PM rb
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...

# re: ASP.NET 2.0 GridView FormatStrings 10/27/2006 9:40 AM Julian Hardy
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!

# re: ASP.NET 2.0 GridView FormatStrings 11/1/2006 11:15 AM Doug Kelley
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!

# Thank you! 11/20/2006 5:43 PM j
Another "thank you" from someone who just couldn't figure out why it wasn't working right.

# re: ASP.NET 2.0 GridView FormatStrings 2/14/2007 4:16 AM Nodar
Many thanks! )

# re: ASP.NET 2.0 GridView FormatStrings 2/22/2007 4:16 PM louis
tank goodness!

# re: ASP.NET 2.0 GridView FormatStrings 3/13/2007 5:47 AM Hasan
Oh thanks for the tip man! I was looking why my grid won't render my text according the format string!

# re: ASP.NET 2.0 GridView FormatStrings 4/26/2007 6:19 PM t
Thx, save my 2 hours

# re: ASP.NET 2.0 GridView FormatStrings 7/17/2007 2:01 AM donkey
thanks man!

# re: ASP.NET 2.0 GridView FormatStrings 7/29/2007 2:39 AM Gul
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.


# re: ASP.NET 2.0 GridView FormatStrings 8/7/2007 8:58 AM Ryan
Thanks and gig em!

# re: ASP.NET 2.0 GridView FormatStrings 8/17/2007 9:53 AM klrfz1
<ItemTemplate>
<%# Server.HtmlEncode( Eval("Price", "{0:c}").ToString() ) %>
</ItemTemplate>


Might work.

# re: ASP.NET 2.0 GridView FormatStrings 9/10/2007 7:38 AM Shankar
Huh! I spent few hours make it to work. But I landed here - Thanks

# re: ASP.NET 2.0 GridView FormatStrings 10/30/2007 12:30 AM Carlito
God bless you Steve for those wonderful four sentences of handy information!!!!!!!!!!!!!!

# re: ASP.NET 2.0 GridView FormatStrings 2/4/2008 10:25 AM John
Thank you very much :)

# re: ASP.NET 2.0 GridView FormatStrings 5/19/2008 3:59 PM Michael
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

# re: ASP.NET 2.0 GridView FormatStrings 5/19/2008 4:20 PM Steve
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?

# re: ASP.NET 2.0 GridView FormatStrings 5/30/2008 10:33 AM Michael
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.


# re: ASP.NET 2.0 GridView FormatStrings 6/17/2008 5:39 AM Jay
Cool! It worked, thanks Steve!.
one question, what dataformatstring should i use to make the field display Php 5,000.00?

# re: ASP.NET 2.0 GridView FormatStrings 7/9/2008 4:48 PM mama
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.

# re: ASP.NET 2.0 GridView FormatStrings 7/14/2008 12:58 PM sallie
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) %>'

#  ASP.NET 2.0 GridView FormatStrings 7/17/2008 6:17 AM ahmed
how to load data in gridview & delete, update date in gridview

Thanks

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

Thanks

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 4 and 5 and type the answer here: