This is a migrated thread and some comments may be shown as answers.

Phone Fromat using DataFormatString

3 Answers 639 Views
Grid
This is a migrated thread and some comments may be shown as answers.
sam varg
Top achievements
Rank 1
sam varg asked on 15 Sep 2010, 07:28 PM

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

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 16 Sep 2010, 04:51 AM
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.
 
0
sam varg
Top achievements
Rank 1
answered on 16 Sep 2010, 02:41 PM
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
0
John
Top achievements
Rank 1
answered on 07 Mar 2020, 07:57 PM

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>

Tags
Grid
Asked by
sam varg
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
sam varg
Top achievements
Rank 1
John
Top achievements
Rank 1
Share this question
or