GridView and Formatting

V

Victor Rodriguez

Why is it that when I setup a BoundField Column and set the DataFormatString
property to {0:C} I get everything in 100.0000 format instead of 100.00?

Thanks in advance,

Victor
 
V

Victor Rodriguez

I've tried both ways and only by creating a fieldtemplate and on the binding
using <%# bind("FieldName","{0:c} %> works but not the other way.

Victor
 
W

Walter Wang [MSFT]

Hi Victor,

Thank you for your post.

First, both "{0:C}" "{0:c}" will do the same since it's not case-sensitive.

The problem you encountered is described in a KB that is not published yet.
Here's some content of it:

KB919282
The value of a BoundField control is not formatted correctly when you set
the value of the DataFormatString member of the BoundField control in an
ASP.NET-connected Web application


SYMPTOMS

When you set the value of the DataFormatString member of a BoundField
control in an ASP.NET-connected Web application, the value of the
BoundField control is not formatted correctly. This problem occurs when the
following conditions are true:
* You are working with an ASP.NET-connencted Web application that is built
on the Microsoft .NET Framework 2.0.
* The application contains a DetailsView control or a GridView control
that in turn contains a BoundField control.
* The value of the HtmlEncode member of the BoundField control is set to
True in order to encode the value of the DataField member of the BoundField
control.
Note By default, the HtmlEncode member of a BoundField control is set to
True .

CAUSE

This problem occurs because the value in the DataField member of the
BoundField control is encoded before formatting is applied. When the value
of the DataField member is encoded, the data type of the value is changed
to String . Because the data type of the value of the DataField member is
changed, the value of the DataField member is not formatted correctly. For
example, if the value of the HtmlEncode member of the BoundField control is
set to True , the value of the BoundField control will be the same as the
result of the following pseudo-code:

String.Format( DataFormatString , HttpUtility.HtmlEncode( DataField ))

However, if the value of the HtmlEncode member of the BoundField control is
set to False , the value of the BoundField control will be the same as the
result of the following pseudo-code:

String.Format( DataFormatString , DataField )

RESOLUTION

To resolve this problem, set the value of the HtmlEncode member of the
BoundField control to False .

STATUS

This behavior is by design.

MORE INFORMATION

For more information about how to use the BoundField control, visit the
following Microsoft Developer Network (MSDN) Web site:
http://msdn2.microsoft.com/en-us/system.web.ui.webcontrols.boundfield_member
s.aspx

For more information about how to use the HtmlEncode member of the
BoundField control, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/system.web.ui.webcontrols.boundfield.htmlen
code.aspx


Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
V

Victor Rodriguez

Excellent job, that's what I need it...

Victor

Walter Wang said:
Hi Victor,

Thank you for your post.

First, both "{0:C}" "{0:c}" will do the same since it's not
case-sensitive.

The problem you encountered is described in a KB that is not published
yet.
Here's some content of it:

KB919282
The value of a BoundField control is not formatted correctly when you set
the value of the DataFormatString member of the BoundField control in an
ASP.NET-connected Web application


SYMPTOMS

When you set the value of the DataFormatString member of a BoundField
control in an ASP.NET-connected Web application, the value of the
BoundField control is not formatted correctly. This problem occurs when
the
following conditions are true:
* You are working with an ASP.NET-connencted Web application that is built
on the Microsoft .NET Framework 2.0.
* The application contains a DetailsView control or a GridView control
that in turn contains a BoundField control.
* The value of the HtmlEncode member of the BoundField control is set to
True in order to encode the value of the DataField member of the
BoundField
control.
Note By default, the HtmlEncode member of a BoundField control is set to
True .

CAUSE

This problem occurs because the value in the DataField member of the
BoundField control is encoded before formatting is applied. When the value
of the DataField member is encoded, the data type of the value is changed
to String . Because the data type of the value of the DataField member is
changed, the value of the DataField member is not formatted correctly. For
example, if the value of the HtmlEncode member of the BoundField control
is
set to True , the value of the BoundField control will be the same as the
result of the following pseudo-code:

String.Format( DataFormatString , HttpUtility.HtmlEncode( DataField ))

However, if the value of the HtmlEncode member of the BoundField control
is
set to False , the value of the BoundField control will be the same as the
result of the following pseudo-code:

String.Format( DataFormatString , DataField )

RESOLUTION

To resolve this problem, set the value of the HtmlEncode member of the
BoundField control to False .

STATUS

This behavior is by design.

MORE INFORMATION

For more information about how to use the BoundField control, visit the
following Microsoft Developer Network (MSDN) Web site:
http://msdn2.microsoft.com/en-us/system.web.ui.webcontrols.boundfield_member
s.aspx

For more information about how to use the HtmlEncode member of the
BoundField control, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/system.web.ui.webcontrols.boundfield.htmlen
code.aspx


Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.
 
W

Walter Wang [MSFT]

Hi Victor,

Appreciate your update and response. I am glad to hear that the problem has
been fixed. If you have any other questions or concerns, please do not
hesitate to contact us. It is always our pleasure to be of assistance.

Have a nice day!

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top