Musings of Geekdom by Eric Newton

tail /var/log/thoughts
posts - 88 , comments - 41 , trackbacks - 68

asp:BoundField DataFormatString problem

A new oddity seems to have cropped up after moving our projects from asp v2.0 b2 to asp v2.0 rc1...

My gridview's have boundfield's with the DataFormatString set to {0:C}.

<asp:gridview runat="server" id="GridView1" width="100%"
OnRowDataBound="GridView1_RowDataBound" autogeneratecolumns="False" cellpadding="6" DataKeyNames="ShoppingCartDetailID,Quantity" DataSourceID="SqlDataSource1"
>
<EmptyDataTemplate>There are no items in the cart.</EmptyDataTemplate>
<headerstyle />
<Columns>
<asp:BoundField headertext="Product Code" DataField="ProductCode" >
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField HeaderText="Description" DataField="Description" />
<asp:BoundField HeaderText="Your Price" DataField="ItemPrice" DataFormatString="{0:C}" >
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:TemplateField HeaderText="Qty">
<ItemTemplate>
<asp:TextBox id="Quantity" runat="server" Text='<%# Eval("Quantity") %>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField HeaderText="Line Total" DataField="ExtendedPrice" DataFormatString="{0:C}">
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
</columns>
</asp:gridview>

Previous to the RC1 build (v2.0.50727) the decimal typed columns were returning the number formatted for currency (US currency: “$0.00“) but now just seems to be ignoring the “C“ format.

However, any routines that are calling the decimal ToString(“C“), and even a regular string.Format(“{0:C}“) works as expected.

Anybody else seen this problem?

Print | posted on Wednesday, October 26, 2005 9:42 PM |

Feedback

Gravatar

# re: asp:BoundField DataFormatString problem

Hi there

Yes, I have seen this problem - I cannot seem to format either decimals or dates or anything for that matter: the result is that the formatting is completely ignored.

Phil
10/31/2005 6:22 AM | Phil Steel
Gravatar

# re: asp:BoundField DataFormatString problem

Same here. asp page using gridview with dataformatstring applies formatting fine under .net 2 beta 2, but stops working in rtm.
very annoying. not the first 'minor' difference I've found.
11/14/2005 9:53 AM | Bill Delaney
Gravatar

# re: asp:BoundField DataFormatString problem

I have exactly the same problem, hope anybody can let us know how to tacke this
11/14/2005 2:09 PM | Ricardo Luzardo
Gravatar

# re: asp:BoundField DataFormatString problem

I have the same problem. Happy to see I am not just crazy. Sad to see I am going to have to go with template columns or something similar.
11/15/2005 1:53 PM | OblioJoe
Gravatar

# re: asp:BoundField DataFormatString problem

I also have the problem. hope sb. can give a solution :(
11/16/2005 7:09 PM | wayne
Gravatar

# re: asp:BoundField DataFormatString problem

Set the HtmlEncode property on the column to False - that should do it for ya.
11/18/2005 8:37 AM | Dustin
Gravatar

# re: asp:BoundField DataFormatString problem

>> Set the HtmlEncode property on the column to False - that should do it for ya.

it's ok! thanks.
11/21/2005 11:58 PM | james
Gravatar

# re: asp:BoundField DataFormatString problem

I did that but didn't work
11/29/2005 3:57 PM | Marlon
Gravatar

# re: asp:BoundField DataFormatString problem

I was using {0:P2} and setting the HtmlEncode="false" worked for me. Thanks!
12/2/2005 12:46 PM | Chris
Gravatar

# re: asp:BoundField DataFormatString problem

Setting HtmlEncode="false" will fix this problem. I don't know why, but it fixes the problem.
1/12/2006 11:59 AM | Ken Sykora
Gravatar

# re: asp:BoundField DataFormatString problem

Unfortunately, setting the HtmlEncode="false" did not work for me either. It appears fine in design view, but on running the app it fails. Hmmmm. Very strange that it works for some and not others.
1/19/2006 2:00 PM | Tim Butler
Gravatar

# re: asp:BoundField DataFormatString problem

Just figured this out. When I selected Columns from the gridview properties and set the DataFormatString to {0:c} it did NOT actually change it in the aspx page. I had previously had it formated using {0:###,###,##0.00}, which was still showing on the aspx page. So I manually changed the code and it worked fine. I noticed the same behavior when trying to change columns widths.
1/19/2006 2:09 PM | Tim Butler
Gravatar

# re: asp:BoundField DataFormatString problem

I have seen HTMLEncode=False work fine on a GridView, but NOT on a DetailsView control.
Any ideas?
2/15/2006 11:03 AM | Eric
Gravatar

# re: asp:BoundField DataFormatString problem

HtmlEncode="false" worked perfectly for the GridView I'm working on, I spent half a dozen hours on this, thanks!!
4/20/2006 12:11 PM | Dave Gelowitz
Gravatar

# re: asp:BoundField DataFormatString problem

Thanks, you just saved me half a dozen hours! It worked like a charm.
5/5/2006 9:51 AM | Ivan
Gravatar

# re: asp:BoundField DataFormatString problem

This is obviously a flaw in .Net. They aren't calling for the format string manipulation in data grid bound controls unless the HtmlEncode property is set to false.

Unfortunately we did spend about an hour playing with data types thinking that the new SQL data type money was causing the issue.

Looking forward to SP1...
5/12/2006 1:24 AM | David Rodecker
Gravatar

# re: asp:BoundField DataFormatString problem

HtmlEncode="false" worked perfectly for the GridView I'm working on. thank you all!!!!!
6/17/2006 9:05 AM | karthick
Gravatar

# re: asp:BoundField DataFormatString problem

THANK YOU!!! Have spent several hours fiddling with the formats etc. etc. feared that i would have to change everything to template fields (which I think are messy).
7/3/2006 11:43 AM | LordWabbit
Gravatar

# re: asp:BoundField DataFormatString problem

HtmlEncode="false" worked worked. Thanks!

7/7/2006 1:10 PM | Gary Tuttle
Gravatar

# re: asp:BoundField DataFormatString problem

HtmlEncode="false" worked for me too. However, I got an error at first because I'd changed my DataFormatString to "${0:c}" like I'd seen on another page I'd found while searching for this fix. If you get this error, just remove the "$" symbol from your DataFormatString and you should be good to go.

Thanks!
7/12/2006 2:13 PM | Bo
Gravatar

# re: asp:BoundField DataFormatString problem

I set HtmlEncode = "false" in Gridview, it works for me. save a lot of time, Thank you
7/13/2006 9:06 PM | cathy
Gravatar

# re: asp:BoundField DataFormatString problem

Thanks a lot! After losing a "few" hours I gave up and started using template fields. At that moment I couldn't find anything on the web.
Now that I found your article I can go back to BoundFields ^_^
7/27/2006 2:16 AM | Eugen Anghel
Gravatar

# re: asp:BoundField DataFormatString problem

thanks.It helped me...
10/4/2006 2:32 AM | Pankit
Gravatar

# re: asp:BoundField DataFormatString problem

http://weblogs.asp.net/rajbk/archive/2005/10/31/429090.aspx
11/19/2006 1:19 PM | Andy
Gravatar

# re: asp:BoundField DataFormatString problem

I figured out how to set the field for the date format. How do I set the field for time? I want to display the hours and minutes only. Thanks in advance.
12/5/2006 9:10 PM | Bill Coyne
Gravatar

# re: asp:BoundField DataFormatString problem

Wow! Awesome, Thank goodness I searched here before wasting hours on the issue. Btw, I don't know why other people had a problem with the DetailsView, setting HtmlEncode="false" in the BoundField worked for me.
1/9/2007 12:04 PM | David
Gravatar

# re: asp:BoundField DataFormatString problem

Phew Sweet. Good old HTML encode.
2/14/2007 6:11 PM | rob
Gravatar

# re: asp:BoundField DataFormatString problem

Helped me too! Thanks for helping when MSDN cannot ;)
3/10/2007 1:59 PM | Des Legumes
Gravatar

# re: asp:BoundField DataFormatString problem

thanks a ton.. i have spent 2 hours too on formatting the decimal, yup html encode works,, Great!!
4/17/2007 6:18 PM | ruchi
Gravatar

# re: asp:BoundField DataFormatString problem

Worked for me too
4/19/2007 11:47 PM | chris hulbert
Gravatar

# re: asp:BoundField DataFormatString problem

Thanx, worked for me 2
4/24/2007 4:45 AM | Heckbart
Gravatar

# re: asp:BoundField DataFormatString problem

This only works on a bound column. I need to use a template column.

Has any one figured this out using a template column?
5/7/2007 7:21 PM | webmonkeymon
Gravatar

# re: asp:BoundField DataFormatString problem

I had same problem displaying gridview with decimals,
I solved that problem by using "{0:C}"
and HtmlEncode=false,
Thank you
7/12/2007 5:39 AM | Murali
Gravatar

# re: asp:BoundField DataFormatString problem

Doesn't work for me in DetailsView, either. More specifically, it DOES work in DetailsView view mode, but when I switch to edit mode, it DOES NOT work.
7/26/2007 10:21 AM | Dan Clem
Gravatar

# re: asp:BoundField DataFormatString problem

turning off html encode works like a gem, thank you
8/1/2007 1:23 PM | andrew
Gravatar

# re: asp:BoundField DataFormatString problem

turning off html encode did the trick. Thank you.
8/2/2007 9:21 AM | Lynn
Gravatar

# re: asp:BoundField DataFormatString problem

turning off html saved my day. Thank you very much.
8/30/2007 10:30 AM | vlad
Gravatar

# re: asp:BoundField DataFormatString problem

For anyone struggling with a DetailsView set the ApplyFormatInEditMode property to true. You don't need the HTMLEncode one.
2/22/2008 4:01 AM | Nix
Gravatar

# re: asp:BoundField DataFormatString problem

For Currency Format use it as:
<asp:BoundField DataField="ChargedAmount" HeaderText="Charged Amount" DataFormatString="{0:C}" HtmlEncode="False" SortExpression="ChargedAmount" />
For Date Format use it as:
<asp:BoundField DataField="ChargedDate" HeaderText="Charged Date" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="False" SortExpression="ChargedDate" />
4/4/2008 10:55 AM | Besnik
Gravatar

# re: asp:BoundField DataFormatString problem

It's not working form me. Any ideas why?
4/8/2008 5:16 PM | Ivonne
Gravatar

# re: asp:BoundField DataFormatString problem

thanks to you all for the answers.
11/18/2008 8:15 AM | meryem
Gravatar

# re: asp:BoundField DataFormatString problem

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.
3/30/2010 12:10 AM | Regeln von Slots
Gravatar

# re: asp:BoundField DataFormatString problem

The site about asp.net that how to solve Bound Field Data Format String Problem
10/12/2010 12:24 AM | sound proof tiles
Gravatar

# re: asp:BoundField DataFormatString problem

Programming language and get good points with this.
10/15/2010 1:17 AM | online casino giochi
Gravatar

# re: asp:BoundField DataFormatString problem

Data format string works on a bound column. I should need to use a template column.
11/24/2010 11:09 PM | wood repair
Gravatar

# re: asp:BoundField DataFormatString problem

overriding the Format DataValue method, worked like a charm
11/29/2010 3:42 AM | 12 passenger van rental
Gravatar

# re: asp:BoundField DataFormatString problem

It;s an informative article thanks for this
12/8/2010 1:06 AM | van rental lax
Comments have been closed on this topic.

Powered by: