Musings of Geekdom by Eric Newton

tail /var/log/thoughts
posts - 88 , comments - 41 , trackbacks - 68

DataFormatString not working in asp:BoundFields

As an update to this post, I ran across this bug filed (and experienced by yours truly) regarding the DataFormatString behavior changing from Beta2 to RTM.

So we have to set the HtmlEncode property to true on EVERY single boundfield now?

UPDATE: ...Just tried it... and its still ignoring the DataFormatString.  Anybody have any suggestions?

Print | posted on Monday, October 31, 2005 4:21 PM |

Feedback

Gravatar

# re: DataFormatString not working in asp:BoundFields

I'm still using Beta2 and haven't upgraded yet so I'll probably be in the same boat as you.

Anyway, here's a possible work-around. Add

OnRowDataBound = "RowDataBound"

to your GridView and add the following to your code-behind:

public void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.DataItemIndex > -1)
{
e.Row.Cells[0].Text = string.Format("{0,C}", e.Row.Cells[0].Text);
}
}

If string.Format STILL doesn't work, at least you can hard code a function to replace it.

HTH Brian
11/1/2005 6:38 AM | Brian
Gravatar

# re: DataFormatString not working in asp:BoundFields

I was hoping to not have to do that, since there's upwards of 10 gridviews with roughly 2-5 columns that require the treatment.
11/2/2005 7:30 AM | Eric Newton
Gravatar

# re: DataFormatString not working in asp:BoundFields

Yes, I am finding the exact same problem. I might consider the workaround suggested (thanks), but would like to avoid it if possible.
11/5/2005 1:47 PM | David Silverlight
Gravatar

# re: DataFormatString not working in asp:BoundFields

I wanted to provide the known fix to this problem just so other people who find this thread via Google (as I did when researching the problem tonight) can resolve the problem.

To get the DataFormatString to work in .NET 2 RTM you need to set the HtmlEncode property to False for each BoundField. The GridView is encoding everything first, which breaks the format function. You have to disable the encoding to get DataFormat to work.

Thanks!
12/21/2005 9:30 PM | Todd Anglin
Gravatar

# re: DataFormatString not working in asp:BoundFields

Thank you so much. The "htmlencode=false" fixed this.
4/10/2008 11:39 AM | Some.Net(Guy)
Gravatar

# re: DataFormatString not working in asp:BoundFields

Thank you! That fixed it for me too. It worked fine without it on my local machine though, just not on the server, any idea why? (The service pack maybe?)
4/17/2008 2:29 PM | Dave
Comments have been closed on this topic.

Powered by: