Blog Stats
  • Posts - 80
  • Articles - 1
  • Comments - 341
  • Trackbacks - 69

 

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?


Feedback

# re: asp:BoundField DataFormatString problem

Gravatar 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

# re: asp:BoundField DataFormatString problem

Gravatar 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

# re: asp:BoundField DataFormatString problem

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

# re: asp:BoundField DataFormatString problem

Gravatar 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

# re: asp:BoundField DataFormatString problem

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

# re: asp:BoundField DataFormatString problem

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

# re: asp:BoundField DataFormatString problem

Gravatar >> 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

# re: asp:BoundField DataFormatString problem

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

# re: asp:BoundField DataFormatString problem

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

# re: asp:BoundField DataFormatString problem

Gravatar 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

# re: asp:BoundField DataFormatString problem

Gravatar 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

# re: asp:BoundField DataFormatString problem

Gravatar 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

# re: asp:BoundField DataFormatString problem

Gravatar 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

# re: asp:BoundField DataFormatString problem

Gravatar 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

# re: asp:BoundField DataFormatString problem

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

# re: asp:BoundField DataFormatString problem

Gravatar 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

# re: asp:BoundField DataFormatString problem

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

# re: asp:BoundField DataFormatString problem

Gravatar 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

# re: asp:BoundField DataFormatString problem

Gravatar HtmlEncode="false" worked worked. Thanks!

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

# re: asp:BoundField DataFormatString problem

Gravatar 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

# re: asp:BoundField DataFormatString problem

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

# re: asp:BoundField DataFormatString problem

Gravatar 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

# re: asp:BoundField DataFormatString problem

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

# re: asp:BoundField DataFormatString problem

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

# re: asp:BoundField DataFormatString problem

Gravatar 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

# re: asp:BoundField DataFormatString problem

Gravatar 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

# re: asp:BoundField DataFormatString problem

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

# re: asp:BoundField DataFormatString problem

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

# re: asp:BoundField DataFormatString problem

Gravatar 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

# re: asp:BoundField DataFormatString problem

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

# re: asp:BoundField DataFormatString problem

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

# re: asp:BoundField DataFormatString problem

Gravatar 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

# re: asp:BoundField DataFormatString problem

Gravatar 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

# re: asp:BoundField DataFormatString problem

Gravatar 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

# re: asp:BoundField DataFormatString problem

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

# re: asp:BoundField DataFormatString problem

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

# re: asp:BoundField DataFormatString problem

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

# re: asp:BoundField DataFormatString problem

Gravatar 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

# re: asp:BoundField DataFormatString problem

Gravatar 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

# re: asp:BoundField DataFormatString problem

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

# re: asp:BoundField DataFormatString problem

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

# re: asp:BoundField DataFormatString problem

Gravatar if it does work in DetailsView edtit mode, set ApplyFormatInEditMode="true" 5/30/2009 2:21 AM | aarron

# re: asp:BoundField DataFormatString problem

Gravatar hello
i have problem in Datagridview
can you help me.

دانلود پروژه کارآفرینی
http://www.bitasoft.ir/ 7/19/2009 5:48 AM | پروژه کارآفرینی

# :BoundField DataFormatString problem

Gravatar Спасибо вам что вы естя 9/23/2009 6:45 AM | Darwin

# BoundField DataFormatString problem

Gravatar неплохой пейзаж 10/2/2009 2:54 AM | Котенок

Post a comment





 

 

 

Copyright © Eric Newton