[RESOLVED] [02/03] Format Phone number in Datagrid BoundField
How do you format a cell to display 999-999-9999 in a Datagrid in VS2003? There is no htmlencode property of the field...
thanks.
Re: [RESOLVED] [02/03] Format Phone number in Datagrid BoundField
Just made a function to handle the string manipulation...
Re: [RESOLVED] [02/03] Format Phone number in Datagrid BoundField
Share your solution with the world. :)
Re: [RESOLVED] [02/03] Format Phone number in Datagrid BoundField
Sorry was a quick solution so I'm sure it could be written better:
vb Code:
Public Function convertPhone(ByVal stNumber As Object) As String
Dim newString As String
Try
newString = Mid(stNumber.ToString, 1, 3) & "-"
newString = newString & Mid(stNumber.ToString, 3, 3) & "-"
newString = newString & Mid(stNumber.ToString, 6, 4)
If newString = "--" Then
newString = ""
End If
Catch ex As Exception
End Try
Return newString
End Function
Code:
<asp:TemplateColumn HeaderText="Phone">
<HeaderStyle Width="100px"></HeaderStyle>
<ItemTemplate>
<%# convertPhone(DataBinder.Eval(Container.DataItem, "srs_Contact_Phone")) %>
</ItemTemplate>
</asp:TemplateColumn>
Re: [RESOLVED] [02/03] Format Phone number in Datagrid BoundField
Oh, I think you could have done this using String.Format in the codebehind, or using the DataFormatString property of the column (bound column only) with something like {0:###-###-####} in it.
Re: [RESOLVED] [02/03] Format Phone number in Datagrid BoundField
Yeah the string.format in code behind probably works. I did try the DataStringFormat on a bound field but there weren't any results so I was thinking it was due to not haveing the HTMLEncode property.