DataFormatString in GridView

I wonder if someone else is suffering the same problem. I found DataFormatString in GridView not work very well when I try to display currency. In VS 2005 Beta version, it works fine, but after I use stardard version, it never display correct. For example: integer 3 always display like 3.0000 but in the beta version, it display like $3.00 which is what I would like. By the way, I assigned datasource, dataadapter and sqlcommand manually, everything else is good, even date format string works, but currency format just NOT work. Not sure if this is a bug?
DataFormatString {0:c2}
[586 byte] By [WeiMing] at [2007-12-17]
# 1
Yes, I, too, have encountered the same thing. Though I didn't work with the beta edition(s), I've been migrating some of my older datagrids to gridviews and have spent the better part of a morning trying to figure out WHY the format strings are NOT working...Tongue Tied
BobKellerJr at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2

Due to a bug fix, a BoundField bound to a DateTime field with HtmlEncode=true (default) will behave differently in Beta 2 than in RTM. This is by design. Because HtmlEncode is on, the data is retrieved from the data store, HtmlEncoded, and then the format string is applied. By HtmlEncoding the value from the data store, we are protecting unsafe script from the data store from being displayed to the client. You can change the behavior for their field by setting HtmlEncode to false if you do not need to HtmlEncode your dates.

The Web Platform and Tools Team

Solved...bug reported and answered up on the MSDN site...

BobKellerJr at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3
Thank you Bob.
WeiMing at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4
I'm suffering the same problem: dataformatstring seems to be not working at all at least for dates. I tried to turn HTMLEncode off without any result.

The only way I have found to format date field is (both in gridview and in detailview) to turn the field into template and then to change the binding expression this way bind(datefield,"{0:d}").

Thanks

Ettore at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5
I am having similar difficulties..... I can not get the dataformatstring, to have any effect on the way the data is displayed in the grid...... all I want to do is limit the number of decimal places to (2) and thought that if I placed the following into the dataformatstring property {0:f2} that I would accomplish the task.......please help me Thanks, Rick
RickH at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 6

If I understand correctly, HtmlEncoded = "false" needs to be included for the DataFormatString to work in the currency and datetime BoundFields.

Unfortunately, this makes formatting in these grids very busy. Now formatting the field requires two steps instead of one. The old DataGrid only required the DataFormatString. Wouldn't it be better if setting the DataFormatString to anything but {0} automatically set the HtmlEncoded to false.

MarkPhillips at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 7
It would seem that this is a recurring problem.

I have been having problems using DataFormatString with boundfields and I've followed all the advice above including setting:

HtmlEncode = false;

I think probably the easiest thing to do is for me to show the code i'm using and see if anyone can spot my stupid mistake.

fld = new BoundField();

fld.HeaderText = "Date";
fld.DataField = "adatefield";
fld.HtmlEncode = false;
fld.DataFormatString = "{0:dd-MMM-yyyy}";
//fld.DataFormatString = "{0:d}";

GridView1.Columns.Add(fld);

The Gridview is bound to a DataTable and the adatefield is formatted as
"2005-11-04 00:00:00.00". I thought that it might not recognise the date format, but I checked that by using the following code and it works fine:

DateTime mydatetime =

Convert.ToDateTime("2005-11-04 00:00:00.000");

string mydebug =

string.Format("{0:dd-MMM-yyyy}", mydatetime);

Any ideas? It seems that the use of HtmlEncode set to false fixes most poeples problems, but it doesn't work for me. I've tried various different DataFormatStrings with no luck. It's fair to say that it's driving me nuts.

All help most gratefully received. Ta

Ron

RON7659 at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 8

this has worked for me on dates but nothing i have tried has work for currency. i have tried turning the html encode off and i have tried converting it to template and i still get the same thing please let me know if you have any other ideas

Thanks

Chris Reiter

chreiter at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 9

If you set HTMLEncoded to false and then use this format string:

{0:$#,##0.00;($#,##0.00);0}

you can get currency to display correctly. I tried this in VB 2005 Standard, on a Web site, and it works.

Lisa Z. Morgan

LisaZ.Morgan at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 10
Shouldn't this have been the other way around: First apply the format string, THEN you do the HtmlEncoding?
jcmikkelsen at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 11

On further experimentation, I found that the currency format {0:c} worked fine once HTMLEncode was turned off in the properties window.

Lisa Morgan

LisaZ.Morgan at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 12

Here is a possible work around for the Ron's date problem. In your query change the date col requested to something like this

...., CONVERT(varchar, colDate, 105) AS colDate, ....

Letting SQL Server format the date correctly for you before it gets hashed up in the HtmlEncoding.

This particular formating style (105) will give you 21-05-2006

Good Luck

msbeal at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 13

I had a similair problem with formatting a phone number. To display the text this does work:

<%#String.Format("{0:(###) ###-####}", Convert.ToInt64(DataBinder.Eval(Container.DataItem, "WorkPhone")))%>

If the value ("WorkPhone" in this case) can be null, you can do this:

<%#IIf(DataBinder.Eval(Container.DataItem, "CellPhone") = "0", "", String.Format("{0:(###) ###-####}", Convert.ToInt64(DataBinder.Eval(Container.DataItem, "CellPhone"))))%>

but your value must return "0" if it is null since the third part of the IIF gets executed either way and will error if it can't convert it.

I would still rather have an easier way....

beerge at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 14
I had spent too long on this--thanks beerge for posting your solution!
johnGa at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...