locked
DisplayFormat or DataType - What is better and why this problem? RRS feed

  • Question

  • User-266149547 posted

    I have tried both of the following methods to format my data as a currency.  I would like to know which is the better approach?  Also, why does it work in details and list mode, but when I switch to edit mode it shows with 4 decimals?

    <DataType(DataType.Currency)> _

    <DisplayFormat(DataFormatString:="{0:c}")> _

    Public BookValue As Object

    Tuesday, February 3, 2009 5:11 PM

Answers

  • User-330204900 posted

    If you want the format to be applied to the Edit mode in the Edit FieldTemplate change: 

    <asp:TextBox ID="TextBox1" runat="server" Text='<%# FieldValueEditString %>' CssClass="droplist"></asp:TextBox>
    

    to:

    <asp:TextBox ID="TextBox1" runat="server" Text='<%# FieldValueString %>' CssClass="droplist"></asp:TextBox>
    

    bear in mind that if you do this in say the Text_Edit FieldTemplate then it will affect all text fields that have formatting applied what you should do is create a custom FieldTEmplate based on this and do the change there applying it the field you want via UIHint.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 4, 2009 5:46 AM
  • User660823006 posted

    Also note that DisplayFormat attribute has a property ApplyFormatInEditMode which if set to true will cause the format to work in edit mode as well. The only caveat to this is the field template must be able to convert the text value back to the proper type. If you run into this issue it really indicates that you should be writing a custom field template for the data type.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 4, 2009 1:41 PM
  • User-1005219520 posted

     

    Private _Birthdate As Object 
    <DisplayFormat(DataFormatString := "{0:d}", ApplyFormatInEditMode := True)> _ 
    Public Property Birthdate() As Object 
        Get 
            Return _Birthdate 
        End Get 
        Set(ByVal value As Object) 
            _Birthdate = value 
        End Set 
    End Property 
      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 4, 2009 2:25 PM
  • User660823006 posted

    This is what I was warning about. The datasource control needs to convert whatever is entered in the field to a double because you have a double in your database. The negative of the FormatString is that once it converts you value into a nice pretty "$5.00" internally the DataSource takes that value and calls Convert.ToDouble on it and that will fail because of the "$" character. This is why you best bet is making your own Currency field template that striips off the leading $ before saving the value back to the datasource. If you look at the Text_Edit this is the ExtractValues call where you would add such logic.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 4, 2009 3:19 PM
  • User-260435932 posted

    You can try to change the field template to be a bit more robust and add some code to ExtractValues:

     
    protected override void ExtractValues(IOrderedDictionary dictionary) {
        object value = ConvertEditedValue(TextBox1.Text);
        Decimal result;
        if (value != null && Decimal.TryParse((string)value, NumberStyles.Any, CultureInfo.CurrentCulture, out result)) {
            value = result;
        }
        dictionary[Column.Name] = value;
    }
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 4, 2009 3:37 PM
  • User-330204900 posted
    String text = "This is Some Text";
    var uppercase = text.ToUpper();
    


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 30, 2009 4:42 AM

All replies

  • User297557456 posted

    Hi,

    First both ways are good enough to be used as per your need,

    and it doesn't work in Edit Mode because when the normal bound field or label is replaced with Editable TextBox it doesn't have that Forcefully Applied format to it. It is directly fetched from the source and rendered.

    Try to define a Edit Template and Format it there....

    Tuesday, February 3, 2009 5:24 PM
  • User-330204900 posted

    If you want the format to be applied to the Edit mode in the Edit FieldTemplate change: 

    <asp:TextBox ID="TextBox1" runat="server" Text='<%# FieldValueEditString %>' CssClass="droplist"></asp:TextBox>
    

    to:

    <asp:TextBox ID="TextBox1" runat="server" Text='<%# FieldValueString %>' CssClass="droplist"></asp:TextBox>
    

    bear in mind that if you do this in say the Text_Edit FieldTemplate then it will affect all text fields that have formatting applied what you should do is create a custom FieldTEmplate based on this and do the change there applying it the field you want via UIHint.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 4, 2009 5:46 AM
  • User-1005219520 posted

    I think a better approach is to modify the code behind for the Text_Edit and add a check for if (metadata.DataType == DataType.Currency) then format as currency. See How to: Customize Data Field Appearance and Behavior For Non-Intrinsic Data Types in the Data Model for a sample.

    This might be something that Dynamic Data should do automatically. I'll check into us changing that at some point.

    Wednesday, February 4, 2009 1:34 PM
  • User660823006 posted

    Also note that DisplayFormat attribute has a property ApplyFormatInEditMode which if set to true will cause the format to work in edit mode as well. The only caveat to this is the field template must be able to convert the text value back to the proper type. If you run into this issue it really indicates that you should be writing a custom field template for the data type.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 4, 2009 1:41 PM
  • User-266149547 posted

    Thanks for all the suggestions.

    Wednesday, February 4, 2009 2:09 PM
  • User-1005219520 posted

    ApplyFormatInEditMode  - Scott's suggestion is the way to go. Let us know if you have any problems with this approach.

    Wednesday, February 4, 2009 2:14 PM
  • User-266149547 posted

    <DisplayFormat(ApplyFormatInEditMode(True)),(DataFormatString:="{0:c}")> _

     I am having difficulty with the syntax.. do you see what I am doing wrong?

    Wednesday, February 4, 2009 2:18 PM
  • User-1005219520 posted

     

    Private _Birthdate As Object 
    <DisplayFormat(DataFormatString := "{0:d}", ApplyFormatInEditMode := True)> _ 
    Public Property Birthdate() As Object 
        Get 
            Return _Birthdate 
        End Get 
        Set(ByVal value As Object) 
            _Birthdate = value 
        End Set 
    End Property 
      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 4, 2009 2:25 PM
  • User-266149547 posted

    Thanks, it does work.  Here are the results when formatted as currency.

    I get

     The field OriginalCost must be a valid Double.

     When using

    <DisplayFormat(DataFormatString:="{0:c}", ApplyFormatInEditMode:=True)> _

    Even if the existing value is unchanged.  Also, It looks like the user will be required to enter the $

     

    Wednesday, February 4, 2009 2:44 PM
  • User660823006 posted

    This is what I was warning about. The datasource control needs to convert whatever is entered in the field to a double because you have a double in your database. The negative of the FormatString is that once it converts you value into a nice pretty "$5.00" internally the DataSource takes that value and calls Convert.ToDouble on it and that will fail because of the "$" character. This is why you best bet is making your own Currency field template that striips off the leading $ before saving the value back to the datasource. If you look at the Text_Edit this is the ExtractValues call where you would add such logic.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 4, 2009 3:19 PM
  • User-260435932 posted

    You can try to change the field template to be a bit more robust and add some code to ExtractValues:

     
    protected override void ExtractValues(IOrderedDictionary dictionary) {
        object value = ConvertEditedValue(TextBox1.Text);
        Decimal result;
        if (value != null && Decimal.TryParse((string)value, NumberStyles.Any, CultureInfo.CurrentCulture, out result)) {
            value = result;
        }
        dictionary[Column.Name] = value;
    }
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 4, 2009 3:37 PM
  • User-464228023 posted

    Whatz the syntax for converting a text field (string) to uppercase, both for display as well as edit in C#?

    Thursday, July 30, 2009 2:51 AM
  • User-330204900 posted
    String text = "This is Some Text";
    var uppercase = text.ToUpper();
    


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 30, 2009 4:42 AM
  • User-464228023 posted

    Great! I used your tip like this :-

    public partial class custodian
    {
        partial void Oncustodian_fnameChanged()
        {
            String text = custodian_fname;
            custodian_fname = text.ToUpper();  
        }
     
    }


     

    and it worked like a charm. Thanks!

    Thursday, July 30, 2009 7:13 AM