简体   繁体   English

在datagridview中格式化电话号码

[英]Format phone number in datagridview

In the application I am working on, I have a TableAdapter, and a RadGridView (Telerik control - their version of the DataGridView). 在我正在处理的应用程序中,我有一个TableAdapter和一个RadGridView(Telerik控件-它们的DataGridView版本)。 The TA is pulling different elements from our database, including PhoneNumber, which is stored as varchar. 该技术支持正在从我们的数据库中提取不同的元素,包括存储为varchar的PhoneNumber。

In my GridView, I would like to display this format as (123) 456-7890. 在我的GridView中,我想将此格式显示为(123)456-7890。 Unfortunately, I have been unable to accomplish this. 不幸的是,我无法做到这一点。

I have tried the following (which is Telerik's recommended method): 我尝试了以下方法(这是Telerik的推荐方法):

radGvUsers.Columns["PhoneNumber"].FormatString = "{0: (###) ###-####}";

This results in the value displaying as it does in the db: 1234567890 结果将显示该值,就像在数据库中一样:1234567890

I know (ok, I think) this is because it is stored as varchar and needs to be converted to a number, but I don't know how or where (ie at the TA or GridView level) best to do this. 我知道(好吧,我认为)这是因为它存储为varchar,需要将其转换为数字,但是我不知道这样做的最佳方式或最佳位置(即TA或GridView级别)。

Any suggestions? 有什么建议么? Feel free to ask more questions if I did not supply enough info! 如果我没有提供足够的信息,请随时提出更多问题!

您可以在SQL语句中将字符串转换为数字

Select Convert(bigint, PhoneNumber) AS PhoneNumber FROM sometable

You can easily do like below. 您可以轻松地执行以下操作。 No need to get data as string from database. 无需从数据库获取数据作为字符串。

radGvUsers.Columns["PhoneNumber"].DefaultCellStyle.Format = "(###) ###-####";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM