Format Phone Number String in Datagrid Column

D

Dave

Greetings,

I have a 10 digit phone number stored as a string in a SQL server table. My
datagrid currently displays it as 1234567890, but I would like to format the
column to read:

(123)-456-7890 or even 123.456.7890

Any suggestions for that one?

Thanks,

-Dave
 
E

Eliyahu Goldin

Dave,

You should use either ItemDataBound or PreRender event in codebehind. Get
the value of the cell, format it in the code and put back to the cell. I
don't think there are any standard formatting capabilities for phone
numbers.

Eliyahu
 
E

Edge

Eliyahu said:
Dave,

You should use either ItemDataBound or PreRender event in codebehind.
Get the value of the cell, format it in the code and put back to the
cell. I don't think there are any standard formatting capabilities
for phone numbers.

Eliyahu

There is a third solution: calling a conversion function in the databinding
block.

This is the databinding block:

<%# myFunction(DataBinder.Eval(Container.DataItem,"Telephone") %>

This is the function for VB.NET:

Function myFunction(o As Object) As String
If(IsDbNull(o)) Return ""
Dim str As String = o.ToString()
Return "(" & str.SubString(0,3) & ")-" & str.SubString(3,3) & "-" &
str.SubString(6,4)
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top