DataFormatString in GridView
DataFormatString {0:c2}
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...
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
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.
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"; GridView1.Columns.Add(fld);
fld.DataField = "adatefield";
fld.HtmlEncode = false;
fld.DataFormatString = "{0:dd-MMM-yyyy}";
//fld.DataFormatString = "{0:d}";
"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
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
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
On further experimentation, I found that the currency format {0:c} worked fine once HTMLEncode was turned off in the properties window.
Lisa Morgan
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
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....