How to format phone number in datagrid

# Thread: How to format phone number in datagrid

1. Senior Member
Join Date
Dec 1969
Posts
258

## How to format phone number in datagrid

Hi<BR><BR>How can I format a phone number that i retrieve from the database (datatype is bigint and format is 1234567890) to the standard - (123)456-7890 inside the bound column of a datagrid using dataformatstring. i know we can do it in Itemdatabound but how to do it using dataformatstring. already tried {0: (###)###-####) and {0: (000)000-0000) and others.<BR><BR>Thanks in advance

2. Senior Member
Join Date
Dec 1969
Posts
11,247

## RE: How to format phone number in datagrid

1) don&#039;t store telephone numbers as and int use a text format in the DB.<BR> reason you are not going to do calculations and it could cause complications with overseas numbers that start with 0.<BR>ok now to format.<BR>I have a function that I use on a string<BR>Function formattel(number)<BR> number1 = strreverse(number)<BR> formattel = strreverse(mid(number1, 11, 15))& " (" & strreverse(mid(number1, 8, 3)& ") & strreverse(mid(number1, 5, 3)& "-" & strreverse(mid(number1, 1, 4)<BR>End Function<BR>Why do I reverse it? well then I can format the number no matter how many digits

3. Senior Member
Join Date
Dec 1969
Posts
258

## RE: How to format phone number in datagrid

Thanks a lot for answering but i need to know if I can format it using DataFormatString in datagrid. I already have created a user-defined function to convert the phone number. <BR><BR>Thanks anyway...

4. Senior Member
Join Date
Dec 1969
Posts
258

## RE: How to format phone number in datagrid

Thanks a lot for answering but i need to know if I can format it using DataFormatString in datagrid. I already have created a user-defined function to convert the phone number. <BR><BR>Thanks anyway...

5. Junior Member
Join Date
Dec 1969
Posts
2

## RE: How to format phone number in datagrid

I&#039;m very interested in a good solution for this as well. I&#039;m currently trying to figure out how to override the dataformatstring so that I can make my own format string {0:phone}. Not much luck figuring that out yet. MSDN has a good article called "Customizing Format Strings" that has useful information. I just can&#039;t figure out how to apply it to overriding the dataformatstring in the datagrid.<BR><BR>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcustomizingformatstrings.asp<BR><BR>My 2nd choice can be done. You could create your own custom DataGridColumn following this example: http://aspnet.4guysfromrolla.com/articles/100202-1.2.aspx<BR>You would basically build your own phone Column:<BR>public class PhoneColumn : BoundColumn<BR><BR>If you figure out a good solution, please post it.

6. Junior Member
Join Date
Dec 1969
Posts
2

## RE: How to format phone number in datagrid

I figured it out, but it wasn&#039;t obvious or easy. <BR>I created a custom column:<BR><BR>public class CustomFormatColumn : BoundColumn<BR>{<BR> public override string DataFormatString <BR> {<BR> get <BR> {<BR> object o = ViewState["DataFormatString"];<BR> if (o != null)<BR> return (string)o;<BR> return String.Empty;<BR> }<BR> set <BR> {<BR> ViewState["DataFormatString"] = value;<BR> OnColumnChanged();<BR> }<BR> }<BR><BR> protected override string FormatDataValue(object dataValue)<BR> {<BR> if((dataValue != null) && (dataValue != System.DBNull.Value))<BR> {<BR> //string formatting = DataFormatString;<BR> if(DataFormatString.Length != 0)<BR> {<BR> return String.Format(new customFormatting.MyFormat(),DataFormatString,new object[]{dataValue.ToString()});<BR> } <BR> }<BR> return dataValue.ToString();<BR> } <BR>}<BR><BR><BR>public class MyFormat : IFormatProvider, ICustomFormatter<BR>{<BR> // String.Format calls this method to get an instance of an<BR> // ICustomFormatter to handle the formatting.<BR> public object GetFormat (Type service)<BR> {<BR> if (service == typeof (ICustomFormatter))<BR> {<BR> return this;<BR> }<BR> else<BR> {<BR> return null;<BR> }<BR> }<BR> // After String.Format gets the ICustomFormatter, it calls this format<BR> // method on each argument.<BR> public string Format (string format, object arg, IFormatProvider provider) <BR> {<BR> if (format == null)<BR> {<BR> return String.Format ("{0}", arg);<BR> }<BR> // If the format is not a defined custom code,<BR> // use the formatting support in ToString.<BR> switch(format.ToLower())<BR> {<BR> case "phone":<BR> return toPhone(arg.ToString());<BR> break;<BR> case "ssn":<BR> return toSSN(arg.ToString());<BR> break;<BR> default:<BR> //If the object to be formatted supports the IFormattable<BR> //interface, then pass the format specifier to the <BR> //objects ToString method for formatting.<BR> if (arg is IFormattable) <BR> {<BR> return ((IFormattable)arg).ToString(format, provider);<BR> } <BR> //If the object does not support IFormattable, then <BR> //call the objects ToString method with no additional<BR> //Formattin. <BR> else if (arg != null) <BR> {<BR> return arg.ToString();<BR> }<BR> break;<BR> }<BR> return arg.ToString();<BR> }<BR>}<BR><BR> public static string toPhone(string strToFormat)<BR> {<BR> strToFormat = strToFormat.Trim();<BR> <BR> if(strToFormat.Length &#060;= 7)<BR> { //use this for a 7 digit number<BR> //search for 3+4 characters using "(\d{3})(\d{4})<BR> //reformat to this "\$1-\$2"<BR> return Regex.Replace(strToFormat,"(\d{3})(\d{4})","\$1-\$2");<BR> }<BR> else //use this for a 10 digit number<BR> {<BR> //search for 3+3+4 characters using "(\d{3})(\d{3})(\d{4})"<BR> //reformat to this "(\$1) \$2-\$3"<BR> return Regex.Replace(strToFormat,"(\d{3})(\d{3})(\d{4})", "(\$1) \$2-\$3");<BR> }<BR> }<BR><BR> public static string toSSN(string strToFormat)<BR> {<BR> strToFormat = strToFormat.Trim();<BR> //search for 3+2+4 characters<BR> //reformat to this "\$1-\$2-\$3"<BR> return Regex.Replace(strToFormat,"(\d{3})(\d{2})(\d{4})", "\$1-\$2-\$3");<BR> }<BR> }

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•