4
所以我在GridView中顯示一個SQL表的結果。一些字段是電話號碼。一個特定的字段可能是一個正常的10位數字,但它也可能是一個4位數字的擴展名。如果是4位數字,我希望至少不要把10位數字的格式放在上面,最多我想在它前面加上Ext:然後是我的數據。這是我到目前爲止。我通常不是程序員,所以這是Visual Studio嚮導和谷歌搜索結果拼湊在一起。 非常感謝您的幫助。Gridview格式字段作爲電話號碼,但一些結果是4位數的擴展名,如何處理?
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Call Destination" SortExpression="CallDestination">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("CallDestination") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# String.Format("{0:(###) ###-####}",Convert.ToInt64(DataBinder.Eval (Container.DataItem, "CallDestination")))%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:OnCallConnectionString %>" SelectCommand="SELECT [TimeStamp], [CallerID], [Accepted], [CallDestination] FROM [OnCallLog]"></asp:SqlDataSource>
</div>
</form>
感謝幫助,我大致明白是怎麼回事,但我對編譯獲得兩個相同的錯誤: 「System.Web.UI.WebControls.GridViewRow.DataItem '不能像方法一樣使用 這是指這一行: string newnumberValue = e.Row.DataItem(「NewNumber」)。ToString();這個是: theLabel.Text = String.Format(「 ); 任何想法? – mloraditch
改爲使用括號,如下所示:'e.Row.DataItem [「NewNumber」]。ToString();',請參閱更新後的答案。 –
和我修好了,不知道這是否是必須的,但添加了這一行:DataRowView rowView =(DataRowView)e.Row.DataItem;並將e.Row.DataItem(「NewNumber」)更改爲rowView [「NewNumber」],並且完美地工作。再次感謝! – mloraditch