MaskedEdit does not exclure mask caracters?

3 posts, 0 answers
  1. Francois
    Francois avatar
    52 posts
    Member since:
    Dec 2010

    Posted 21 May 2013 Link to this post

    Hi,

    I'm trying to add a phone number column using the MaskedEditColumn and store it in a nullable double property of a business object, without the mask. However, the code is crashing the I'm leaving the cell with an exception about DataConversion. I found that the value that the grid is trying to set in the PhoneDouble property contains the mask, so the conversion fails. How can I make it work? I thought the MaskFormat = MaskFormat.ExcludePromptAndLiterals would fix that but it doesn't.


    I'm using v.2013.1.220.40

    Thanks!

    --
    Frank



    public static class GridUtils
    {
        public static GridViewMaskBoxColumn AddPhoneNumberColumn(this RadGridView grid, string uniqueName, string fieldName, string headerText, Type type)
        {
            GridViewMaskBoxColumn col = new GridViewMaskBoxColumn();
            col.Name = uniqueName;
            col.FieldName = fieldName;
            col.HeaderText = headerText;
            col.MaskType = MaskType.Standard;
            col.Mask = "(000) 000-0000";
            col.FormatString = "{0:(###) ###-####}";
            col.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
            col.DataType = type;
     
            grid.Columns.Add(col);
     
            return col;
        }
    }
     
    public class Form2 : Form
    {
        private BindingList<TestClass> _dataSource;
     
        public class TestClass
        {
            private string _string;
            private double? _double;
            public string PhoneString
            {
                get { return this._string; }
                set { this._string = value; }
            }
     
            public double? PhoneDouble
            {
                get { return this._double; }
                set { this._double = value; }
            }
        }
     
        public Form2()
        {
            InitializeComponent();
     
            this.radGridView1.AutoGenerateColumns = false;
            this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            this.radGridView1.AddPhoneNumberColumn("stringMasked", "PhoneString", "PhoneStringMasked", typeof(string));
            this.radGridView1.AddPhoneNumberColumn("doubleMasked", "PhoneDouble", "PhoneDoubleMasked", typeof(double?));
     
            this._dataSource = new BindingList<TestClass>();
            this._dataSource.Add(new TestClass() { PhoneDouble = 5554443333, PhoneString = "5554443333" });
     
            this.radGridView1.DataSource = this._dataSource;
        }
     
        #region Designer
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
     
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
     
        #region Windows Form Designer generated code
     
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.radGridView1 = new Telerik.WinControls.UI.RadGridView();
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit();
            this.SuspendLayout();
            //
            // radGridView1
            //
            this.radGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.radGridView1.Location = new System.Drawing.Point(0, 0);
            this.radGridView1.Name = "radGridView1";
            this.radGridView1.Size = new System.Drawing.Size(607, 284);
            this.radGridView1.TabIndex = 0;
            this.radGridView1.Text = "radGridView1";
            //
            // Form2
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(607, 284);
            this.Controls.Add(this.radGridView1);
            this.Name = "Form2";
            this.Text = "Form2";
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit();
            this.ResumeLayout(false);
     
        }
     
        private Telerik.WinControls.UI.RadGridView radGridView1;
        #endregion
        #endregion
     
    }
  2. Francois
    Francois avatar
    52 posts
    Member since:
    Dec 2010

    Posted 24 May 2013 Link to this post

    Hi,

    I'm expecting an answer from Telerik on this.

    Thanks!
  3. Peter
    Admin
    Peter avatar
    1157 posts

    Posted 24 May 2013 Link to this post

    Hi Francois,

    Thank you for writing.

    This requirement cannot be achieved out-of-the-box. You have to handle the CellFormatting event:

    this.radGridView1.CellFormatting += new CellFormattingEventHandler(radGridView1_CellFormatting);
    this.radGridView1.DataSource = this._dataSource;

     
      void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
      {
          if (e.CellElement.ColumnInfo is GridViewMaskBoxColumn)
          {
              long result = 0;
              if (e.CellElement.Text!=null &&( e.CellElement.Text.Contains("(") ||!long.TryParse(e.CellElement.Text, out result)))
              {
                  return;
              }
              e.CellElement.Text = String.Format("{0:(000) 000-0000}", result);
          }
      }

    Please note that you should not set the DataType property for this column type because the GridViewMaskBoxColumn internally expect a text value.

    Refer to the attached project for more information.

    Regards,
    Peter
    Telerik
    RadChart for WinForms is obsolete. Now what?
Back to Top