I
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; }
}
}
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; }
}
}