Groups | Blog | Home
all groups > asp.net webcontrols > november 2005 >

asp.net webcontrols : BoundField and DataFormatString in ASP.NET 2.0 - Bug ?


Phillip Williams
11/29/2005 8:18:02 PM
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

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


[quoted text, click to view]
Ing. Winkler Bernhard
11/30/2005 1:11:08 AM
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; }
}
}
Ing. Winkler Bernhard
12/2/2005 12:00:00 AM
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"
[quoted text, click to view]
Iain
12/7/2005 7:50:40 AM
Thanks for the update Bernhard. I have also had this problem with a web
site ported from beta 2. Setting HtmlEncode="false" fixed.

Iain

[quoted text, click to view]
AddThis Social Bookmark Button