2013-08-28 14 views
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> 

回答

1

您需要使用RowDataBound事件,因爲它綁定到網格攔截每一行因此可以判斷,如果手機號碼爲10位或4位,並根據具體情況逐案處理每個值的基礎上,像這樣:

標記:

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" 
    AutoGenerateColumns="False" onrowdatabound="GridView1_RowDataBound"> 

注:從<asp:Label><ItemTemplate>取出Text='<%# String.Format("{0:(###) ###-####}",Convert.ToInt64(DataBinder.Eval (Container.DataItem, "CallDestination")))%>',因爲你會設置文本格式,並設置在t時的Text財產他以事件而不是聲明。

代碼隱藏:

protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e) 
{ 
    // Only interested in each data row, not header or footer, etc. 
    if(e.Row.RowType == DataControlRowType.DataRow) 
    { 
     // Find the Label2 control in the row 
     Lable theLabel = (Label)e.row.FindControl("Label2"); 

     // Make sure control is not null 
     if(theLabel != null) 
     { 
      // Cast the bound to an object we can use to extract the value from 
      DataRowView rowView = (DataRowView)e.Row.DataItem; 

      // Get the value for CallDestination field in data source 
      string callDestinationValue = rowView["CallDestination"].ToString(); 

      // Find out if CallDestination is 10 digits or 4 digits 
      if(callDestinationValue.Length == 10) 
      { 
       theLabel.Text = String.Format("{0:(###) ###-####}", Convert.ToInt64(rowView["CallDestination"])); 
      } 
      if(callDestinationValue.Length == 4) 
      { 
       theLabel.Text = "Ext: " + callDestinationValue; 
      } 
     } 
    } 
} 
+0

感謝幫助,我大致明白是怎麼回事,但我對編譯獲得兩個相同的錯誤: 「System.Web.UI.WebControls.GridViewRow.DataItem '不能像方法一樣使用 這是指這一行: string newnumberValue = e.Row.DataItem(「NewNumber」)。ToString();這個是: theLabel.Text = String.Format(「 ); 任何想法? – mloraditch

+1

改爲使用括號,如下所示:'e.Row.DataItem [「NewNumber」]。ToString();',請參閱更新後的答案。 –

+0

和我修好了,不知道這是否是必須的,但添加了這一行:DataRowView rowView =(DataRowView)e.Row.DataItem;並將e.Row.DataItem(「NewNumber」)更改爲rowView [「NewNumber」],並且完美地工作。再次感謝! – mloraditch