I need to format a string as a phone number in a radgrid winform cell

Sounds silly simply, right?

I can format the input, but I have a database that is returning the value as a string. No matter what I do if it is stored in the database as 1234567890 I get 1234567890 back. Everything in the database is stored as strictly string numeric data, meaning that there are no letters or special characters, but it is a varchar(10) field.

I have created a GridViewMaskBox column and set it to Numeric, then in the formatting event changed the data to numeric like this:
GridViewMaskBoxColumn maskBoxColumn = new GridViewMaskBoxColumn();
        maskBoxColumn.Name = "Phone";
        maskBoxColumn.FieldName = "cellphone_number";
        maskBoxColumn.HeaderText = "Phone";
        maskBoxColumn.MaskType = MaskType.Numeric;
        maskBoxColumn.Mask = "(000) 000-0000";
        maskBoxColumn.TextAlignment = ContentAlignment.MiddleCenter;
        maskBoxColumn.ReadOnly = false;
        radGridView1.MasterTemplate.Columns.Add(maskBoxColumn);

Open in new window

Then in the cell Formatting event I  have tried two different things:
if (e.Column.Name=="cellPhone")
            {
                long cellInfo =Convert.ToInt64(e.CellElement.Value);
                 e.CellElement.Value = cellInfo;
            }

//I even tried just for testing purpose:
      if (e.Column.Name=="cellPhone")
            {
                long cellInfo =Convert.ToInt64(e.CellElement.Value);
                long number = 1234567890;
                 e.CellElement.Value = number;
            }

Open in new window



and the cell still shows 123456789 instead of (123) 456-7890

I have tried creating a standard text column and using every variation of: radGridView1.Columns["cellPhone"].FormatString = "{0:(###) ###-####}"; then again in the formatting event changing from string to a int64. I have verified in the debugger that it is indeed hitting the event and changing the data type correctly.

I am ready to make small animal sacrifices but fear that will not actually help and may hinder any good karma I have built up. Please help. I have spent hours on what should have been childs play.

Joe

Update: I can do the below which seems to work, but actually replaces the text in the grid with the "()" in the numbers which screws the database update on edit:

if (e.CellElement.Value.ToString().Length==10)
                {
                    BigInteger bi = new BigInteger();
                       bool worked = BigInteger.TryParse(e.CellElement.Value.ToString(),out bi);
                       string numberAsText = bi.ToString("###-###-####");
                       e.CellElement.Value = numberAsText.ToString();}

Open in new window


I have posted this other places and I am running out of time on this, I need to move onto other items.
Thank you in advance...
Joe
Joe RuderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Randy DownsOWNERCommented:
Maybe this will help.

DataFormatString property for phone number will only works if your data type in that column is of type like numeric. This approach does not work if you have data type as string. Now the option is converting the data type of that column to something that can be formatted using the format string. You can try it in ItemDataBound event. Check out the following code snippet

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridDataItem)
       {
           GridDataItem item = (GridDataItem)e.Item;
           Object ob = item["columnPhoneNumber"].Text;
           if (!Convert.IsDBNull(ob))
           {
               Int64 iParsedValue = 0;
               if (Int64.TryParse(ob.ToString(), out iParsedValue))
               {
                   TableCell cell = item["columnPhoneNumber"];
                   cell.Text = String.Format(System.Globalization.CultureInfo.CurrentCulture,
                      "{0:(###) ###-####}", new object[] { iParsedValue });
               }
           }
       }
   }

Open in new window

Joe RuderAuthor Commented:
Randy,
Thank you, but if you basically just recoded what I already have.  maybe you missed that I have code up there that converts it to a bigint successfully, but it is having problems with the database binding then.

For now what I am doing is I have created a new column called cellMasked and I am just populating it thus:

 e.Row.Cells["cellMasked"].Value = Convert.ToUInt64(e.CellElement.Value).ToString("###-###-####");

Still working it though but I think this is on the right track, it would still seem like we should be able to format a string data with just numbers in it back to the same cell, but I guess with the databinding that makes sense.
Joe

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Randy DownsOWNERCommented:
Since you are using an intermediate column you won't have issues maintaining the integrity of your original data.
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Joe RuderAuthor Commented:
yes, but that way of doing things could get unweildly, no?

I wonder if this is a case where going back and refactoring my database would make more sense.

Joe
Randy DownsOWNERCommented:
It would definitely be better to have the phone numbers stored in the proper number format. You may have to edit reports & forms to accommodate the changes.
Joe RuderAuthor Commented:
I used a work around solution.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.