Ticket T297444
Visible to All Users

How to apply a (DisplayFormat) Mask to a value shown in a ListView?

created 9 years ago

I apply a mask dynamically to a fieldvalue. In the editor mode this all works fine, bith in inline edit as well as detailview. The actual value is stored 'as is', i.e. without mask literals.
When the value is shown in a regular listview, it is shown as is, i.e. without the mask. How can I show the value with the mask applied in the listview?

Thanks.

C#
protected override void OnViewControlsCreated() { base.OnViewControlsCreated(); if (View is ListView) { var gridListEditor = ((ListView)View).Editor as ASPxGridListEditor; var propertyEditor = gridListEditor.FindPropertyEditor("Reference", ViewEditMode.Edit) as ASPxPropertyEditor; propertyEditor.EditMaskType = EditMaskType.Simple; propertyEditor.EditMask = referenceMask; propertyEditor.DisplayFormat = String.Format("{{0:{0}}}", referenceMask); if (propertyEditor.Control == null) propertyEditor.ControlCreated += ReferencePropertyEditor_ControlCreated; else SetMaskSettings(propertyEditor); } else if (View is DetailView) { var propertyEditor = ((DetailView)View).FindItem("WorkOrderReferenceID") as ASPxPropertyEditor; propertyEditor.EditMaskType = EditMaskType.Simple; propertyEditor.EditMask = referenceMask; propertyEditor.DisplayFormat = String.Format("{{0:{0}}}", referenceMask); if (propertyEditor.Control == null) propertyEditor.ControlCreated += ReferencePropertyEditor_ControlCreated; else SetMaskSettings(propertyEditor); } } private void ReferencePropertyEditor_ControlCreated(object sender, EventArgs e) { SetMaskSettings(sender as ASPxPropertyEditor); } private static void SetMaskSettings(ASPxPropertyEditor sender) { var editor = ((ASPxTextBox)sender.Editor); editor.MaskSettings.IncludeLiterals = MaskIncludeLiteralsMode.None; }

Answers approved by DevExpress Support

created 9 years ago (modified 9 years ago)

Hello Ad,

If I understand your issue correctly, you can solve it using the ASPxGridListEditor.CustomizeGridViewDataColumn event as shown below:

C#
using System; using System.Collections.Generic; using System.Linq; using DevExpress.ExpressApp; using DevExpress.ExpressApp.Editors; using DevExpress.ExpressApp.Web.Editors.ASPx; using DevExpress.Web; using MainDemo.Module.BusinessObjects; namespace MainDemo.Module.Web.Controllers { public class T297444 : ObjectViewController<ListView, DemoTask> { ASPxGridListEditor listEditor = null; string dynamicDisplayFormat = "g"; protected override void OnActivated() { base.OnActivated(); listEditor = View.Editor as ASPxGridListEditor; if(listEditor != null) { listEditor.CustomizeGridViewDataColumn += listEditor_CustomizeGridViewDataColumn; } } protected override void OnViewControlsCreated() { base.OnViewControlsCreated(); if(listEditor != null) { //Dennis: For inline-editing: var propertyEditor = listEditor.FindPropertyEditor("DueDate", ViewEditMode.Edit) as ASPxPropertyEditor; if(propertyEditor != null) { propertyEditor.DisplayFormat = string.Format("{{0:{0}}}", dynamicDisplayFormat); propertyEditor.EditMask = dynamicDisplayFormat; } } } private void listEditor_CustomizeGridViewDataColumn(object sender, CustomizeGridViewDataColumnEventArgs e) { if(e.ModelColumn.Id == "DueDate") { DateEditProperties propertiesDateEdit = (((GridViewDataDateColumn)(e.Column))).PropertiesDateEdit; propertiesDateEdit.EditFormatString = propertiesDateEdit.DisplayFormatString = string.Format("{{0:{0}}}", dynamicDisplayFormat); ; } } protected override void OnDeactivated() { base.OnDeactivated(); if(listEditor != null) { listEditor.CustomizeGridViewDataColumn -= listEditor_CustomizeGridViewDataColumn; listEditor = null; } } } }

BTW, would you please clarify why you need to change formatting dynamically? With that, we will be in a better position to describe all available options to you. Thanks.

    Show previous comments (6)
    Dennis Garavsky (DevExpress) 9 years ago

      Ad,

      It seems that your columns are storing strings and not numbers.
      The DisplayFormatString format setting is not supposed to work here, as it would not work for String.Format here. Your current mask and display form is supposed to work if the column value is a number.
      And even in this case, it is more correct to use the appropriate numeric format specifier as per https://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx, e.g. W###-#### (there is no built-in numeric specifier like 9, which is a DevExpress thing). If you cannot make your data numeric, you can handle the ASPxGridView.CustomColumnDisplayText event and format your text value manually. e.g., as per http://stackoverflow.com/questions/5908200/string-format-is-not-formating-phone-number. I hope this now makes sense.

        Hi Dennis, thanks for the explanation. I followed your suggestion (convert the string to a numeric) and I am now able to format the value as expected :)
        Thanks for the support.

        Dennis Garavsky (DevExpress) 9 years ago

          You're always welcome, Ad!

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.