Pat*_*ick 1 asp.net data-binding formatting gridview string-formatting
我看到另一个线程有点像我的问题:
但我不知道它是否回答了我的问题,因为他正在使用代码隐藏来制作列.我所做的就是在visual studio中插入GridView控件.顺便说一句,数据正在网格中填充,我只想尝试获取格式设置.
我正在使用Microsoft Visual Studio Professional 2010.(也是我的数据库的SQL Management Studio,但可能不需要这些信息,只是试图给予足够的信息以确保我正在做的事情被理解)
我正在使用Visual Basic.net代码在ASP.NET中创建一个网站.
该网站基本上是一个联系人列表网站.
3个文本框字段.名字,姓氏,主要电话#.
添加记录按钮(从文本框中获取信息并插入数据库)
GridView,显示正在填充信息的数据库
我有一个"主要电话号码"列,这会拉出一个电话号码以显示在GridView中.这个数字只有10位数,没有格式化...(即999-999-9999)
我想让GridView取9999999999并制作它(999)999-9999
如果我查看DataFormatString,我尝试了许多"{0:(###)### - ####}"的组合,包含和不包含引号,还包括全零而不是井号.
通过我的研究,似乎如果我想使用DataFormatString,我需要在我的数据库中将我的电话号码设为int.所以我删除了我的表并将其从varchar重新创建为int.我通过单击Gridview任务(GridView右上角的箭头)到达DataFormatString ...然后"编辑列"...然后在"选定的字段"下单击列的名称..."主电话号码"然后在"CommandField属性"下,我向下滚动到"DataFormatString".
我希望我不是太详细.我非常感谢所有的帮助.
我找到了这个:
http://www.tek-tips.com/viewthread.cfm?qid=328173
但我不知道我将如何使用它..看看因为我的代码是由Visual Studio完成的......有些看起来像这样
更新:我最初发布了错误的代码,无论是哪种方式,基于我所说的Kelsey能够为我提出的答案对我有用.
下面是我的新代码以及凯利给出的更正.
<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>
Run Code Online (Sandbox Code Playgroud)
由于您没有发布您的GridView代码,我必须假设您的列使用BoundField类似这样或类似的东西:
<Columns>
<asp:BoundField DataField="MainPhoneNumber" DataFormatString="{0:(###) ###-####}" />
Run Code Online (Sandbox Code Playgroud)
由于它是一个字符串,DataFormatString因此您无法使用该属性,因此您需要将您更改BoundField为a TemplateField.只需将BoundField以下内容替换为以下内容即可TemplateField:
<asp:TemplateField>
<ItemTemplate>
<asp:Literal ID="litPhone" runat="server" Text='<%# string.Format("{0:(###) ###-####}", Int64.Parse(Eval("MainPhoneNumber").ToString())) %>' />
</ItemTemplate>
</asp:TemplateField>
Run Code Online (Sandbox Code Playgroud)
这里的关键是评估数据绑定字段并将其转换Int64为字符串格式化器的代码.请注意,我使用的是a Int64而不仅仅是int32位,因为10位数字不适合.
我测试了它,它的工作原理:)