Home All Groups Group Topic Archive Search About

How can I format a telephone # in the datagrid

Author
7 Mar 2005 7:38 PM
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.

Author
8 Mar 2005 1:55 AM
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




Show quoteHide quote
"Jeff Thur" <j***@emscirc.com> wrote in message
news:51f201c5234d$365521d0$a601280a@phx.gbl...
> 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.
Author
11 Mar 2005 7:20 PM
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

Show quoteHide quote
"Jeff Thur" <j***@emscirc.com> wrote in message
news:51f201c5234d$365521d0$a601280a@phx.gbl...

> 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.