How can I format a telephone # in the datagrid

J

Jeff Thur

The telephone # is my database is in nnnnnnnnnn format.
When I bind it, it shows the same hard to read format in
the datagrid. How can I make it more user friendly and
show as nnn-nnn-nnnn.
Thanks for any help.
 
K

Ken Cox [Microsoft MVP]

Hi Jeff,

There are a couple of ways to handle this, depending on whether you've
stored the phone numbers as numbers or as strings.

If they are numbers, you can format them quite readily. If they are strings,
you can convert the string to a number and then do the formatting. (That
seems like a hack but it works.)

Here's some demo code (below) that shows the various techniques including a
helper function. Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

<asp:datagrid id="DataGrid1" runat="server"
AutoGenerateColumns="False">
<columns>
<asp:templatecolumn>
<itemtemplate>
<asp:Label runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.phone", "{0:###-###-####}") %>'>
</asp:label>
</itemtemplate>
</asp:templatecolumn>
<asp:boundcolumn DataField="phone" ReadOnly="True"
DataFormatString="{0:###-###-####}"></asp:boundcolumn>
<asp:templatecolumn>
<itemtemplate>
<asp:Label runat="server" Text='<%#
FixPhone(DataBinder.Eval(Container, "DataItem.phone")) %>'>
</asp:label>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Public Function FixPhone _
(ByVal strNumber As String) As String
Dim mynum As String
mynum = "5551234567"
Return Format(Convert.ToDouble(mynum), _
"(###) ###-####")
End Function
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("ADateTime", GetType(DateTime)))
dt.Columns.Add(New DataColumn _
("phone", GetType(Long)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = Now.Date.AddDays(i)
dr(1) = 4165551210 + i
dr(2) = "Item " + i.ToString()
dr(3) = 1.23 * (i + 1)
dr(4) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function
 
P

Paul D. Fox

If your using stored procedures to populate the datagrid, you can modify the
procedure to use a scaler function to reformat the phone number;

CREATE FUNCTION dbo.fn_FormatTelephoneNumber (@sPhone CHAR(10))
RETURNS VARCHAR(15)
AS
BEGIN
DECLARE @sPhoneFormat VARCHAR(15)
IF LEN(@sPhone) < 10
SET @sPhoneFormat = @sPhone
ELSE SET @sPhoneFormat = '(' + LEFT(@sPhone, 3) + ') ' +
SUBSTRING(@sPhone, 4, 3) + '-' + RIGHT(@sPhone, 4)
RETURN @sPhoneFormat
END


Paul
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top