Format Phone Number String in Datagrid Column

Discussion in 'ASP .Net Datagrid Control' started by Dave, Dec 5, 2004.

  1. Dave

    Dave Guest

    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
     
    Dave, Dec 5, 2004
    #1
    1. Advertising

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

    "Dave" <> wrote in message
    news:...
    > 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
    >
    >
     
    Eliyahu Goldin, Dec 6, 2004
    #2
    1. Advertising

  3. Dave

    Edge Guest

    Eliyahu Goldin wrote:
    > 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

    --

    Edge
     
    Edge, Dec 6, 2004
    #3
  4. Dave

    Dave Guest

    Perfect! Thank you both for your help. My column is now properly
    formatted.

    -Dave

    "Edge" <> wrote in message
    news:%237zc2$...
    > Eliyahu Goldin wrote:
    >> 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
    >
    > --
    >
    > Edge
    >
    >
     
    Dave, Dec 6, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dino Chiesa [MSFT]

    Re: Phone Number Format

    Dino Chiesa [MSFT], Jul 29, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    2,164
    Eddy Soeparmin
    Jul 30, 2003
  2. Shahram Khosraviani

    Re: Phone Number Format

    Shahram Khosraviani, Jul 29, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    415
    Shahram Khosraviani
    Jul 29, 2003
  3. Chris Jackson

    Re: Phone Number Format

    Chris Jackson, Jul 29, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    377
    Chris Jackson
    Jul 29, 2003
  4. Mike

    format phone number

    Mike, Jun 1, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    2,806
    Steven Cheng[MSFT]
    Jun 4, 2004
  5. Fei Liu
    Replies:
    21
    Views:
    2,464
    John Bokma
    Dec 16, 2006
Loading...

Share This Page