Connect with me
Search
Twitter Feed
Navigation
« Debug cryptic WCF errors with WCF Tracing and the Microsoft Service Trace Viewer | Main | jQuery 1.3 released! »
Friday
Jan232009

Dynamic Data - Formatting Values

I had a quick question from Justin King just now about how to format a money field to be currency in ASP.NET Dynamic Data.

There are a few ways to do this. You can specify the format at design time by using the DataFormatString property on the DynamicControl

Or you can specify this on the model itself through metadata and the DisplayFormatAttribute

public partial class OrderMetaData
{
	[DisplayFormat(DataFormatString = "{0:c}", 
				 ApplyFormatInEditMode = false, 
				 NullDisplayText="No value specified", 
				 ConvertEmptyStringToNull = true)]
	public object OrderTotal { get; set; }

Either of these will give you nicely formatted fields. The DisplayFormatAttribute method is preferred as it ensures that the format is consistent wherever the field is used, where are the DisplayFormat property on the DynamicControl will only be for that page.

References (6)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
    Response: ctibfbhb
    ctibfbhb
  • Response
    Response: qrtwywtv
    qrtwywtv
  • Response
    Response: zestril
    zestril
  • Response
    Response: wiklsvye
    wiklsvye
  • Response
    Response: hyxexhwf
    hyxexhwf
  • Response
    Response: deqlcpqx
    deqlcpqx

Reader Comments (1)

What if I have my own class that inherits from IFormatProvider? How do I tell the DataFormatString that it should use my custom provider?

Thanks!

20 March 2010 | Unregistered CommenterDeborahK

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>