Home All Groups Group Topic Archive Search About

BoundField and DataFormatString in ASP.NET 2.0 - Bug ?

Author
30 Nov 2005 12:11 AM
Ing. Winkler Bernhard
Hello all,

I found an interesting phenomenon with an incorrect interpretation of
the DataFormatString in a BoundField in ASP.NET 2.0.

In the example below the prices 4, 5, 6 and 7 should all be equally
formatted with the Euro sign and 2 decimal digits. That is done as
expected with the .NET Framework Beta 2 (Build 2.0.50215).

But using the Release (Build 2.0.50727) "...{0:...}..." seems to be
interpreted as "...{0}...". Prices 2 and 5 are formatted like price 1
without Euro sign and with varying decimal digits. Price 4 is
formatted like price 3 with Euro sign and with varying decimal digits.
That seems to be a bug in the Release of the .NET Framework 2.0.

Can anyone reproduce this phenomenon or has a better rework than using
TepmlateFields like for prices 6 and 7?


Bernhard Winkler



TestDataFormatString.aspx:

<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="TestDataFormatString.aspx.cs"
Inherits="TestDataFormatString" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Test attribute DataFormatString of control
BoundField</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="Product"
HeaderText="Product">
                </asp:BoundField>
                <asp:BoundField DataField="Price" HeaderText="Price
1">
                    <ItemStyle HorizontalAlign="Right" />
                </asp:BoundField>
                <asp:BoundField DataField="Price" HeaderText="Price 2"
DataFormatString="{0:C}">
                    <ItemStyle HorizontalAlign="Right" />
                </asp:BoundField>
                <asp:BoundField DataField="Price" HeaderText="Price 3"
DataFormatString="€ {0}">
                    <ItemStyle HorizontalAlign="Right" />
                </asp:BoundField>
                <asp:BoundField DataField="Price" HeaderText="Price 4"
DataFormatString="€ {0:#,###,##0.00}">
                    <ItemStyle HorizontalAlign="Right" />
                </asp:BoundField>
                <asp:BoundField DataField="Price" HeaderText="Price 5"
DataFormatString="{0:€ #,###,##0.00}">
                    <ItemStyle HorizontalAlign="Right" />
                </asp:BoundField>
                <asp:TemplateField HeaderText="Price 6">
                    <ItemTemplate>
                        € <%# ((decimal)DataBinder.Eval(Container,
"DataItem.Price")).ToString("#,###,##0.00") %>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Right" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Price 7">
                    <ItemTemplate>
                        <%# ((decimal)DataBinder.Eval(Container,
"DataItem.Price")).ToString("€ #,###,##0.00") %>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Right" />
                </asp:TemplateField>
            </Columns>
            <PagerSettings Visible="False" />
        </asp:GridView>
    </form>
</body>
</html>



TestDataFormatString.aspx.cs:

using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class TestDataFormatString : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            List<ProductEntity> products = new List<ProductEntity>();
            products.Add(new ProductEntity("Product A",
(decimal)123456));
            products.Add(new ProductEntity("Product B",
(decimal)12345.6));
            products.Add(new ProductEntity("Product C",
(decimal)1234.56));
            products.Add(new ProductEntity("Product D",
(decimal)123.456));

            GridView1.DataSource = products;
            GridView1.DataBind();
        }
    }
}



App_Code/ProductEntity.cs:

using System;

public class ProductEntity
{
    private string _product;
    private decimal _price;

    public ProductEntity(string product, decimal price)
    {
        this._product = product;
        this._price = price;
    }

    public string Product
    {
        get { return this._product; }
    }

    public decimal Price
    {
        get { return this._price; }
    }
}

Author
30 Nov 2005 4:18 AM
Phillip Williams
Hi Ing.

I copied your GridView markup into the same sample in which I tested setting
the first column css display attribute to none and low and behold… I got the
same result that you got.   But then I realized that the problem was simple. 
I have turned one column display invisible but left its header visible.  This
shifted the headers one column to the right.  When I turned all columns that
were hidden to visible, I got all of your date formats displayed correctly. 
I know it is a far shot but could it be that you have one column turned
invisible while its header is still visible.  If it is not the case then you
might want to know that your code worked fine on my release version 2.0.50727

Show quoteHide quote
"Ing. Winkler Bernhard" wrote:

> Hello all,
>
> I found an interesting phenomenon with an incorrect interpretation of
> the DataFormatString in a BoundField in ASP.NET 2.0.
>
> In the example below the prices 4, 5, 6 and 7 should all be equally
> formatted with the Euro sign and 2 decimal digits. That is done as
> expected with the .NET Framework Beta 2 (Build 2.0.50215).
>
> But using the Release (Build 2.0.50727) "...{0:...}..." seems to be
> interpreted as "...{0}...". Prices 2 and 5 are formatted like price 1
> without Euro sign and with varying decimal digits. Price 4 is
> formatted like price 3 with Euro sign and with varying decimal digits.
> That seems to be a bug in the Release of the .NET Framework 2.0.
>
> Can anyone reproduce this phenomenon or has a better rework than using
> TepmlateFields like for prices 6 and 7?
>
>
> Bernhard Winkler
>
>
>
> TestDataFormatString.aspx:
>
> <%@ Page Language="C#" AutoEventWireup="true"
> CodeFile="TestDataFormatString.aspx.cs"
> Inherits="TestDataFormatString" %>
>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
>
> <html xmlns="http://www.w3.org/1999/xhtml" >
> <head runat="server">
>     <title>Test attribute DataFormatString of control
> BoundField</title>
> </head>
> <body>
>     <form id="form1" runat="server">
>         <asp:GridView ID="GridView1" runat="server"
> AutoGenerateColumns="False">
>             <Columns>
>                 <asp:BoundField DataField="Product"
> HeaderText="Product">
>                 </asp:BoundField>
>                 <asp:BoundField DataField="Price" HeaderText="Price
> 1">
>                     <ItemStyle HorizontalAlign="Right" />
>                 </asp:BoundField>
>                 <asp:BoundField DataField="Price" HeaderText="Price 2"
> DataFormatString="{0:C}">
>                     <ItemStyle HorizontalAlign="Right" />
>                 </asp:BoundField>
>                 <asp:BoundField DataField="Price" HeaderText="Price 3"
> DataFormatString="€ {0}">
>                     <ItemStyle HorizontalAlign="Right" />
>                 </asp:BoundField>
>                 <asp:BoundField DataField="Price" HeaderText="Price 4"
> DataFormatString="€ {0:#,###,##0.00}">
>                     <ItemStyle HorizontalAlign="Right" />
>                 </asp:BoundField>
>                 <asp:BoundField DataField="Price" HeaderText="Price 5"
> DataFormatString="{0:€ #,###,##0.00}">
>                     <ItemStyle HorizontalAlign="Right" />
>                 </asp:BoundField>
>                 <asp:TemplateField HeaderText="Price 6">
>                     <ItemTemplate>
>                         € <%# ((decimal)DataBinder.Eval(Container,
> "DataItem.Price")).ToString("#,###,##0.00") %>
>                     </ItemTemplate>
>                     <ItemStyle HorizontalAlign="Right" />
>                 </asp:TemplateField>
>                 <asp:TemplateField HeaderText="Price 7">
>                     <ItemTemplate>
>                         <%# ((decimal)DataBinder.Eval(Container,
> "DataItem.Price")).ToString("€ #,###,##0.00") %>
>                     </ItemTemplate>
>                     <ItemStyle HorizontalAlign="Right" />
>                 </asp:TemplateField>
>             </Columns>
>             <PagerSettings Visible="False" />
>         </asp:GridView>
>     </form>
> </body>
> </html>
>
>
>
> TestDataFormatString.aspx.cs:
>
> using System;
> using System.Data;
> using System.Configuration;
> using System.Collections.Generic;
> using System.Web;
> using System.Web.Security;
> using System.Web.UI;
> using System.Web.UI.WebControls;
> using System.Web.UI.WebControls.WebParts;
> using System.Web.UI.HtmlControls;
>
> public partial class TestDataFormatString : System.Web.UI.Page
> {
>     protected void Page_Load(object sender, EventArgs e)
>     {
>         if (!this.IsPostBack)
>         {
>             List<ProductEntity> products = new List<ProductEntity>();
>             products.Add(new ProductEntity("Product A",
> (decimal)123456));
>             products.Add(new ProductEntity("Product B",
> (decimal)12345.6));
>             products.Add(new ProductEntity("Product C",
> (decimal)1234.56));
>             products.Add(new ProductEntity("Product D",
> (decimal)123.456));
>
>             GridView1.DataSource = products;
>             GridView1.DataBind();
>         }
>     }
> }
>
>
>
> App_Code/ProductEntity.cs:
>
> using System;
>
> public class ProductEntity
> {
>     private string _product;
>     private decimal _price;
>
>     public ProductEntity(string product, decimal price)
>     {
>         this._product = product;
>         this._price = price;
>     }
>
>     public string Product
>     {
>         get { return this._product; }
>     }
>
>     public decimal Price
>     {
>         get { return this._price; }
>     }
> }
>
>
Author
2 Dec 2005 2:11 AM
Ing. Winkler Bernhard
Hi Phillip,

Today I deployed my posted showcase to a multiple test and production
servers (Win 2003) and it's always the same. With Beta 2 formatting is
always as expected and with the release custom formats in
DataFormatString are always ignored for any data type (decimal, int,
DateTime already tested).

The help tells that DataFormatString will be ignored unless the
containing control is in read only mode. The GridView control has no
ReadOnly attribute. So I understand this information as "not in edit
mode".

And here comes the solution:
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring.aspx

I added HtmlEncode="false" to the BoundField and all works fine with
the release.


Bernhard Winkler



On Tue, 29 Nov 2005 20:18:02 -0800, "Phillip Williams"
<Phillip.Willi***@webswapp.com> wrote:

Show quoteHide quote
>Hi Ing.
>
>I copied your GridView markup into the same sample in which I tested setting
>the first column css display attribute to none and low and behold… I got the
>same result that you got.   But then I realized that the problem was simple. 
>I have turned one column display invisible but left its header visible.  This
>shifted the headers one column to the right.  When I turned all columns that
>were hidden to visible, I got all of your date formats displayed correctly. 
>I know it is a far shot but could it be that you have one column turned
>invisible while its header is still visible.  If it is not the case then you
>might want to know that your code worked fine on my release version 2.0.50727
Author
7 Dec 2005 3:50 PM
Iain
Thanks for the update Bernhard. I have also had this problem with a web
site ported from beta 2. Setting HtmlEncode="false" fixed.

Iain

Ing. Winkler Bernhard wrote:
Show quoteHide quote
> Hi Phillip,
>
> Today I deployed my posted showcase to a multiple test and production
> servers (Win 2003) and it's always the same. With Beta 2 formatting is
> always as expected and with the release custom formats in
> DataFormatString are always ignored for any data type (decimal, int,
> DateTime already tested).
>
> The help tells that DataFormatString will be ignored unless the
> containing control is in read only mode. The GridView control has no
> ReadOnly attribute. So I understand this information as "not in edit
> mode".
>
> And here comes the solution:
> http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring.aspx
>
> I added HtmlEncode="false" to the BoundField and all works fine with
> the release.
>
>
> Bernhard Winkler
>
>
>
> On Tue, 29 Nov 2005 20:18:02 -0800, "Phillip Williams"
> <Phillip.Willi***@webswapp.com> wrote:
>
> >Hi Ing.
> >
> >I copied your GridView markup into the same sample in which I tested setting
> >the first column css display attribute to none and low and behold... I got the
> >same result that you got.   But then I realized that the problem was simple.
> >I have turned one column display invisible but left its header visible.  This
> >shifted the headers one column to the right.  When I turned all columns that
> >were hidden to visible, I got all of your date formats displayed correctly.
> >I know it is a far shot but could it be that you have one column turned
> >invisible while its header is still visible.  If it is not the case then you
> >might want to know that your code worked fine on my release version 2.0.50727