Current .Net Version: 2.0.50727
Home
Articles
C# & .Net Framework Reference
Practice C# Online
XML Web Services
RSS Feeds
Code Snippets
T-SQL Scripts
Videos
Valuable Links
Contact Us
Mike's C#, VS.NET and SQL Blog




ROR XML Info.
BoundField's DataFormatString Attribute Doesn't Work As Expected
Author: Michael G.     Last Updated:10/19/2006 8:09:04 PM
Category(ies): ASP.NET/Web Forms , .Net Framework 2.0
Description: There is a issue where if you add a DataFormatString attribute to a GridView's BoundField tag, or other bound column that supports this attribute, the formatting specified will not be rendered. Learn how to fix that problem.
  Add & View Comments About this Article

There is a issue where if you add a DataFormatString attribute to a GridView's BoundField tag, or other bound column that supports this attribute, the formatting specified will not be rendered. That's because the behavior of the CLR is to apply HTML encoding before any special formatting is specified, overriding any custom formatting, and not allowing the formatting to influence the field's presented data.

To resolve this issue, simply add the HtmlEncode attribute to the BoundField's tag and give it a boolean false value.

<asp:gridview id="dgDotNetFun" runat="Server" autogeneratecolumns="false" allowpaging="True" allowsorting="True"> <columns> <asp:boundfield datafield="CustomerID" visible="false"/> <asp:boundfield datafield="Created" headertext="Created" sortexpression="Created" headerstyle-horizontalalign="Left" itemstyle-horizontalalign="Left" headerstyle-width="15%" htmlencode="false" dataformatstring="{0:M/dd/yy h:mm tt}"/> <asp:boundfield datafield="CustomerFirstName" headertext="First Name" sortexpression="CustomerFirstName" headerstyle-horizontalalign="Left" itemstyle-horizontalalign="Left" headerstyle-width="13%"/> <asp:boundfield datafield="CustomerLastName" headertext="Last Name" sortexpression="CustomerLastName" headerstyle-horizontalalign="Left" itemstyle-horizontalalign="Left" headerstyle-width="13%"/> </columns> </asp:gridview>

Comments Click Here to Add your Comment
By Gaurav @ 12/3/2006 5:22:37 AM

Thats cool stuff.. After trying for 2 hrs... i got ur Idea............... Thanks a lot.......
By nCad @ 3/6/2007 5:20:12 PM

I also tried this one and it works. Thanks a lot.
By ugemvidy @ 9/21/2007 4:44:25 PM

>a href="http://bqzlcwfo.com"<lkourjek>/a<  buxdzjyi http://bxqjtrcn.com tjvggcjk xtvweslx  [URL=http://osjuztsf.com]pgsndnta[/URL]
By rqqbzwym @ 11/4/2007 4:04:48 AM

>a href="http://qtuoowir.com"<gamwwhzd>/a<  [URL=http://itjknyyn.com]vstwdhgp[/URL] ; qmsfnozs http://thdpqspg.com hekelqzk uhtvcloy
By wdtflfxk @ 11/4/2007 8:22:47 AM

>a href="http://ayzwysfa.com"<nhcettxg>/a<  [URL=http://hwqownyr.com]ucnoddby[/URL] ; fnyopion http://yfnbsqty.com zsspcrsc zicwfrwj
By njdvpsjv @ 2/13/2008 9:22:53 PM

>a href="http://imaghzjb.com"<golicwzr>/a<  [URL=http://kiapxtff.com]owypoito[/URL] ; kmkhrtxo http://fgqieaeq.com apsaqyhq hvskbtnx
 
COPYRIGHT/DISCLAIMER | CONTACT US