Professional Web Applications Themes

How can I format a telephone # in the datagrid - ASP.NET Data Grid Control

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

  1. #1

    Default How can I format a telephone # in the datagrid

    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.
    Jeff Thur Guest

  2. #2

    Default Re: How can I format a telephone # in the datagrid

    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




    "Jeff Thur" <jeffemscirc.com> wrote in message
    news:51f201c5234d$365521d0$a601280aphx.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.
    Ken Cox [Microsoft MVP] Guest

  3. #3

    Default Re: How can I format a telephone # in the datagrid

    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

    "Jeff Thur" <jeffemscirc.com> wrote in message
    news:51f201c5234d$365521d0$a601280aphx.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.

    Paul D. Fox Guest

Similar Threads

  1. Format DataGrid Row
    By EdLaFave in forum Macromedia Flex General Discussion
    Replies: 1
    Last Post: April 23rd, 02:53 PM
  2. Fixing the format of my datagrid
    By daniel kurtz in forum ASP.NET Data Grid Control
    Replies: 1
    Last Post: October 11th, 01:42 PM
  3. Replies: 1
    Last Post: November 25th, 08:05 AM
  4. Format datagrid in code behind
    By Poppy in forum ASP.NET Data Grid Control
    Replies: 2
    Last Post: June 30th, 05:01 PM
  5. Format Dates in DataGrid
    By Steve C. Orr, MCSD in forum ASP.NET General
    Replies: 0
    Last Post: July 26th, 12:42 AM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139