格式化GridView中的电话号码 [英] Format Telephone Number in GridView

查看:126
本文介绍了格式化GridView中的电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到另一个线程有点像我的问题:

I see another thread somewhat like my question at:

ASP.NET GridView列格式化电话号码

但是我不知道是否回答我的问题,因为他正在使用代码隐藏来创建列。我所做的就是将GridView控件插入视觉工作室。 BTW,数据正在网格中填充,我只是想要现在获得格式设置。

but i do not know if it answers my question as he is using code-behind to make the colum. All I did was insert the GridView control in visual studio. BTW, the data is being populated in the Grid, I am just trying to get the formatting set now.

我正在使用Microsoft Visual Studio Professional 2010.(另外SQL管理Studio为我的数据库,但这个信息可能不需要,只是试图给足够的,以确保我正在做的是被理解)

I am using Microsoft Visual Studio Professional 2010. (Also SQL Management Studio for my database but this information may not be needed, just trying to give enough to make sure what i am doing is understood)

我正在ASP中制作一个网站。 NET与Visual Basic.net代码背后。

I am making a website in ASP.NET with Visual Basic.net code behind.

该网站基本上是联系人列表网站。

The site is basically a contact list site.

3文本框字段。名字,姓氏,主要电话号码。

3 Text Box Fields. First Name, Last Name, Main Phone #.

添加记录按钮(从文本框中获取信息并插入到数据库中)

Add Record Button (Takes the information from the text boxes and inserts into a database)

GridView显示正在填充信息的数据库

GridView that shows the database that is being populated with the information

我有一个主电话号码列,并将一个电话号码在GridView中显示。这个数字只有10位数,没有格式化...(即999-999-9999)

I have a "Main Phone Number" Column and this pulls a telephone number to show in GridView. The number is only 10 digits, no formatting...(i.e. 999-999-9999)

我试图让GridView取9999999999并使其(999) 999-9999

I am trying to make GridView take the 9999999999 and make it (999) 999-9999

如果我看看DataFormatString,我尝试过许多组合{0:(###)### - ####}没有引号,也没有引号,而且都是零,而不是英镑。

If I look at the DataFormatString I have tried many combinations of "{0:(###) ###-####}" with and without the quotations and also with all zeroes instead of pound signs.

通过我的研究,似乎如果我想使用DataFormatString,我需要在我的电话号码数据库一个int。所以我删除了我的表,并将其从varchar重新创建为int。我通过点击Gridview任务(GridView右上角的箭头)...然后编辑列...然后在选定的字段下进入DataFormatString我点击列的名称...主电话号码然后在CommandField属性下面,我向下滚动到DataFormatString。

Through my research it seemed that if I want to use DataFormatString I need to make my phone number in my database an int. So I deleted my table and re-created it from a varchar to an int. I get to the DataFormatString by clicking on Gridview Tasks (arrow at top right of GridView)... then "Edit columns"... then under "Selected Fields" I click the name of the column... "Main Phone Number" then on the under "CommandField properties" I scroll down to "DataFormatString".

我希望我不太详细。我真的非常感谢所有的帮助。

I hope I am not being too detailed. I have really appreciated all the help.

我发现这个:

http://www.tek-tips.com/viewthread.cfm?qid=328173

但我不知道我会如何使用它..看到如何因为我的代码是由Visual Studio完成...其中一些看起来像这样

but i don't know how i would go about using it.. seeing as how because my code was done by Visual Studio... some of it looks like this

更新:我最初发布错误的代码,无论哪种方式,基于我所说的凯尔西能够为我建议一个答案

UPDATE: I posted the wrong code originally, either way, based off what I stated Kelsey was able to suggest an answer for me that worked.

以下是我的新代码,并提供了Kelly给予的更正。

Below is my new code WITH the corrections that Kelly gave.

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="EmpId" DataSourceID="SqlDataSource1" 
            EmptyDataText="There are no data records to display." CellPadding="4" 
        ForeColor="#333333" GridLines="None" Height="136px" Width="299px" 
              AllowSorting="True">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                <asp:BoundField DataField="EmpId" HeaderText="EmpId" ReadOnly="True" 
                    SortExpression="EmpId" Visible="False" />
                <asp:BoundField DataField="FirstName" HeaderText="First Name" 
                    SortExpression="FirstName" />
                <asp:BoundField DataField="LastName" HeaderText="Last Name" 
                    SortExpression="LastName" />
<%--                <asp:BoundField DataField="MainPhoneNumber" HeaderText="Main Phone Number" 
                    SortExpression="MainPhoneNumber" />--%>
                    <asp:TemplateField HeaderText="Main Phone Number"> 
                <ItemTemplate> 
                 <asp:Literal ID="litPhone"  runat="server" Text='<%# string.Format("{0:(###) ###-####}", Int64.Parse(Eval("MainPhoneNumber").ToString())) %>' /> 
                </ItemTemplate> 
                </asp:TemplateField> 

            </Columns>
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <SortedAscendingCellStyle BackColor="#FDF5AC" />
            <SortedAscendingHeaderStyle BackColor="#4D0000" />
            <SortedDescendingCellStyle BackColor="#FCF6C0" />
            <SortedDescendingHeaderStyle BackColor="#820000" />
        </asp:GridView>


推荐答案

由于您没有发布您的 GridView 代码我必须假设您的列正在使用这样或类似的 BoundField

Since you didn't post your GridView code I have to assume that your columns are using a BoundField like this or something similar:

<Columns>
    <asp:BoundField DataField="MainPhoneNumber" DataFormatString="{0:(###) ###-####}" />

由于它是一个字符串,您不能使用 DataFormatString property,因此您需要将 BoundField 更改为 TemplateField 。只需用 BoundField 替换以下 TemplateField ,它应该可以工作:

Since it is a string, you can't use the DataFormatString property so you will need to change your BoundField to a TemplateField. Just replace your BoundField with the following TemplateField and it should work:

<asp:TemplateField>
    <ItemTemplate>
        <asp:Literal ID="litPhone" runat="server" Text='<%# string.Format("{0:(###) ###-####}", Int64.Parse(Eval("MainPhoneNumber").ToString())) %>' />
    </ItemTemplate>
</asp:TemplateField>

这里的关键是对数据绑定字段进行评估的代码,并将其转换为 Int64 ,以使字符串格式化程序工作。请注意,我正在使用一个 Int64 ,而不仅仅是一个$ code> int ,它是32位,因为10位数字将不适合。

The key here is the code that evaluates the databound field and converts it to an Int64 so that the string formatter will work. Note that I am using an Int64 and not just an int which is 32 bit because a 10 digit number will not fit.

我已经测试过,它的工作原理:)

I have tested it and it works :)

这篇关于格式化GridView中的电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆