Phone Fromat using DataFormatString

4 posts, 0 answers
  1. sam varg
    sam varg avatar
    20 posts
    Member since:
    Feb 2010

    Posted 15 Sep 2010 Link to this post

    How do I format the phone number like (999) 999-9999 in a Grid

     

    I did as below. But it return as 9999999999.


     

    <telerik:GridBoundColumn DataField="PhoneNumber" SortExpression="PhoneNumber" 
    HeaderText="Contact Number" UniqueName="columnPhoneNumber" 
    DataFormatString = "{0:(###)###-####}" HeaderStyle-Width="100px"
    <HeaderStyle HorizontalAlign="Center" /> 
    </telerik:GridBoundColumn>


    Thanks
    Sam

  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 15 Sep 2010 Link to this post

    Hello Sam,

    The DataFormatString property for phone number will only works if your data type in that column is of type like numeric. This approach does not work if you have data type as string. Now the option is converting the data type of that column to something that can be formatted using the format string. You can try it in ItemDataBound event. Check out the following code snippet.

    C#:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
       {
           if (e.Item is GridDataItem)
           {
               GridDataItem item = (GridDataItem)e.Item;
               Object ob = item["columnPhoneNumber"].Text;
               if (!Convert.IsDBNull(ob))
               {
                   Int64 iParsedValue = 0;
                   if (Int64.TryParse(ob.ToString(), out iParsedValue))
                   {
                       TableCell cell = item["columnPhoneNumber"];
                       cell.Text = String.Format(System.Globalization.CultureInfo.CurrentCulture,
                          "{0:(###) ###-####}", new object[] { iParsedValue });
                   }
               }
           }
       }

    Thanks,
    Princy.
     
  3. sam varg
    sam varg avatar
    20 posts
    Member since:
    Feb 2010

    Posted 16 Sep 2010 Link to this post

    Thanks for your quick reply. I managed to get it format by below code. I am not sure whether this is a good solution. Do you have any idea performance wise which one will be better. Using a Template column or writing code in ItemDataBound.


    <telerik:GridTemplateColumn HeaderText="Phone Number">
        <HeaderStyle Width="60px" />
        <ItemTemplate>
        <%# string.Format("{0:(###)###-####}", Convert.ToInt64(Eval("PhoneNumber"))) %>                                                   
        </ItemTemplate>
    </telerik:GridTemplateColumn>

    Thanks
    Sam
  4. John
    John avatar
    4 posts
    Member since:
    Nov 2013

    Posted 07 Mar Link to this post

    I know, late to the party, but here's what I do.

    Store the value in db as varchar(10)

    In columns

    <telerik:GridTemplateColumn HeaderText="Telephone" UniqueName="Telephone" AllowFiltering="True"
    Exportable="True" SortExpression="Telephone" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" ShowFilterIcon="false" FilterControlWidth="96%">
    <ItemTemplate>
    <asp:Label runat="server" ID="lblTelephone" Text='<%# iif(isdbnull(Eval("Telephone")), "", String.Format("{0:(###) ###-####}",Convert.ToInt64(iif(isdbnull(Eval("Telephone")),0,DataBinder.Eval(Container.DataItem, "Telephone"))))) %>' />
    </ItemTemplate>
    </telerik:GridTemplateColumn>

Back to Top