在GridView控件格式电话号码 [英] Format Telephone Number in GridView

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

问题描述

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

<一个href=\"http://stackoverflow.com/questions/1180403/asp-net-gridview-column-formatting-telephone-number\">http://stackoverflow.com/questions/1180403/asp-net-gridview-column-formatting-telephone-number

但我不知道这是否因为他是用code-背后,使柱回答我的问题。我所做的只是插入Visual Studio中的GridView控件。 BTW,数据被在网格填充,我只是试图让格式化设置完毕。

我使用Microsoft Visual Studio 2010专业版(也SQL Management Studio中对我的数据库,但可能并不需要这些信息,只是想给予足够的,以确保我在做什么了解)

我正在在ASP.NET网站使用Visual Basic.net code后面。

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

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

添加记录按钮(注意到从文本框和插入的信息到数据库)

GridView控件的显示正在填入信息的数据库

我有一个主电话号码一栏,这拉的电话号码在GridView控件来显示。数量只有10个数字,没有格式...(即999-999-9999)

我试图让GridView控件采取9999999999,使它(999)999-9999

如果我看DataFormatString我已经尝试了多种组合{0:(###)### - ####}。有和没有报价,并与全零,而不是英镑符号

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

我希望我不是太详细。我真的AP preciated所有帮助。

我发现这一点:

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

但我不知道我怎么会去使用它..看到如何,因为我的code是由Visual Studio完成......一些它看起来像这样


更新:我贴错了code最初,无论哪种方式,根据关我说凯尔西是能够提出我的答案工作

下面是与凯利给我改正新的code。

 &LT; ASP:GridView控件ID =GridView1=服务器的AutoGenerateColumns =FALSE
            的DataKeyNames =EMPID的DataSourceID =SqlDataSource1
            EmptyDataText =有没有数据记录显示。 CELLPADDING =4
        前景色=#333333网格=无HEIGHT =136pxWIDTH =299px
              AllowSorting =真&GT;
            &LT; AlternatingRowStyle背景色=白/&GT;
            &LT;柱体和GT;
                &LT; ASP:CommandField中的ShowDeleteButton =真ShowEditButton =真/&GT;
                &LT; ASP:BoundField的数据字段=EMPID的HeaderText =EMPID只读=真
                    SORTEX pression =EMPID可见=FALSE/&GT;
                &LT; ASP:BoundField的数据字段=名字的HeaderText =名
                    SORTEX pression =名字/&GT;
                &LT; ASP:BoundField的数据字段=姓氏的HeaderText =姓
                    SORTEX pression =姓氏/&GT;
&LT;% - &LT; ASP:BoundField的数据字段=MainPhoneNumber的HeaderText =电话号码
                    SORTEX pression =MainPhoneNumber/&GT; - %GT;
                    &LT; ASP:的TemplateField的HeaderText =电话号码&GT;
                &LT;&ItemTemplate中GT;
                 &LT; ASP:文字ID =litPhone=服务器文本='&LT;%#的String.Format({0:(###)### - ####},Int64.Parse(的eval(MainPhoneNumber)的ToString()))%方式&gt;' /&GT;
                &LT; / ItemTemplate中&GT;
                &LT; / ASP:的TemplateField&GT;            &LT; /专栏&GT;
            &LT; FooterStyle背景色=#990000FONT-粗体=真前景色=白/&GT;
            &LT; HeaderStyle背景色=#990000FONT-粗体=真前景色=白/&GT;
            &LT; PagerStyle背景色=#FFCC66前景色=#333333Horizo​​ntalAlign =中心/&GT;
            &LT; RowStyle的BackColor =#FFFBD6前景色=#333333/&GT;
            &LT; SelectedRowStyle背景色=#FFCC66FONT-粗体=真前景色=海军/&GT;
            &LT; SortedAscendingCellStyle背景色=#FDF5AC/&GT;
            &LT; SortedAscendingHeaderStyle背景色=#4D0000/&GT;
            &LT; SortedDescendingCellStyle背景色=#FCF6C0/&GT;
            &LT; SortedDescendingHeaderStyle背景色=#820000/&GT;
        &LT; / ASP:GridView的&GT;


解决方案

既然你没有张贴您的 GridView控件 code我必须假设你的列使用的BoundField 这样的或类似的东西:

 &LT;柱体和GT;
    &LT; ASP:BoundField的数据字段=MainPhoneNumberDataFormatString ={0:(###)### - ####}/&GT;

由于它是一个字符串,则不能使用 DataFormatString 属性,因此,你需要改变你的的BoundField 模板列。只需更换你的的BoundField 具有以下模板列,它应该工作:

 &LT; ASP:的TemplateField&GT;
    &LT;&ItemTemplate中GT;
        &LT; ASP:文字ID =litPhone=服务器文本='&LT;%#的String.Format({0:(###)### - ####},Int64.Parse(的eval(MainPhoneNumber)的ToString()))%方式&gt;' /&GT;
    &LT; / ItemTemplate中&GT;
&LT; / ASP:的TemplateField&GT;

这里的关键是code,用于评估数据绑定字段并将其转换为的Int64 这样的字符串格式化会工作。请注意,我使用的是的Int64 并不仅仅是一个 INT 这是32位的,因为一个10位的数字将不适合

我已经测试它和它的作品:)

I see another thread somewhat like my question at:

http://stackoverflow.com/questions/1180403/asp-net-gridview-column-formatting-telephone-number

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.

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)

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

The site is basically a contact list site.

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 that shows the database that is being populated with the information

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)

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

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.

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.

I found this:

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

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.

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>

解决方案

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:(###) ###-####}" />

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>

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天全站免登陆