This is a migrated thread and some comments may be shown as answers.

gridview masked text box seems broken

3 Answers 122 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 2
Joe asked on 25 Apr 2015, 12:57 PM

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

 

 

3 Answers, 1 is accepted

Sort by
0
Joe
Top achievements
Rank 2
answered on 27 Apr 2015, 01:09 PM

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

 

 

0
Joe
Top achievements
Rank 2
answered on 27 Apr 2015, 01:11 PM

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

Sorry.

0
Ralitsa
Telerik team
answered on 29 Apr 2015, 08:11 AM
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.

 
Tags
GridView
Asked by
Joe
Top achievements
Rank 2
Answers by
Joe
Top achievements
Rank 2
Ralitsa
Telerik team
Share this question
or