gridview masked text box seems broken

4 posts, 0 answers
  1. Joe
    Joe avatar
    26 posts
    Member since:
    Feb 2012

    Posted 25 Apr 2015 Link to this post

    I know this sounds silly, but no matter what I do when I pull data into a masked text box on the grid it ignores my mask.

    I have a grid, radGridview1.  My data is stored in the database as string data.

    I am adding the column in code:

    GridViewMaskBoxColumn maskBoxColumn = new GridViewMaskBoxColumn();
                maskBoxColumn.Name = "Phone";
                maskBoxColumn.FieldName = "cellphone_number";
                maskBoxColumn.HeaderText = "Phone";
                maskBoxColumn.MaskType = MaskType.Numeric;
                maskBoxColumn.Mask = "(999) 000-0000";
                maskBoxColumn.TextAlignment = ContentAlignment.BottomRight;
                radGridView1.MasterTemplate.Columns.Add(maskBoxColumn);

    Then in the CellFormating event:

    if (e.Column.Name=="Phone")
             {
                 long cellInfo =Convert.ToInt64(e.CellElement.Value);
                 e.CellElement.Value = cellInfo;
             }
     

    In the debugger it stops on this line fine, I can confirm that cellInfo has the phone number stored in it (6185551212) say.

    But in the cell it ignores the mask.

    I have tried using type of standard and numeric. I have created columns in the columns editor.

    I attached a picture of what it is doing.

    Any thoughts?

    Thanks,

    Joe

     

     

  2. Joe
    Joe avatar
    26 posts
    Member since:
    Feb 2012

    Posted 27 Apr 2015 in reply to Joe Link to this post

    Telerik support has answered this and suggests using a type converter:

    http://www.telerik.com/help/winforms/gridview-columns-converting-data-types.html

    The solution I am using for this, as it is only one column is to create a new column called cellMask that is not databound, then turn the visibility off on the original column, then do a conversion:

    The code - in the rowFormatting eveng:

    if (e.RowElement != null)
                    {
                        //get the cellphone number
                        var cellNumber = e.RowElement.RowInfo.Cells["cellPhone"].Value.ToString();
                        Int64 valueAs64bit = 0;
                        if (Int64.TryParse(cellNumber, out valueAs64bit))
                        {
                            e.RowElement.RowInfo.Cells["cellMasked"].Value =
                                string.Format("{0:(###) ###-####}", new object[]
                                { Convert.ToUInt64(e.RowElement.RowInfo.Cells["cellPhone"].Value) });
                        }
                 
                    }

    Hope this helps somebody else as well.

    Joe

     

     

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Joe
    Joe avatar
    26 posts
    Member since:
    Feb 2012

    Posted 27 Apr 2015 in reply to Joe Link to this post

    I can't seem to close this question out as answered.

    Sorry.

  5. Ralitsa
    Admin
    Ralitsa avatar
    176 posts

    Posted 29 Apr 2015 Link to this post

    Hi Joe,

    Thank you for contacting us. 

    The events like RowFormatting and CellFormatting are fired every time when the cell`s visual state needs to be updated. If you use the snippet in the handler of the formatting event this can cause performance issues.   

    I attached the samplefor type converter again to be available in the forum post as well.

    Hope this will help you. Let me know if you have any other questions.

    Regards,
    Ralitsa
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
Back to Top