C# Gridview格式字段作为电话号码,但有些结果是4位分机,如何处理?

C# Gridview格式字段作为电话号码,但有些结果是4位分机,如何处理?,c#,asp.net,gridview,C#,Asp.net,Gridview,因此,我在gridview中显示来自SQL表的结果。有些字段是电话号码。特定字段可以是普通的10位数字,但也可以是4位扩展名。如果它是一个4位数的数字,我想至少不要在上面加上10位数的格式,最多我想在它前面加上Ext:,然后加上我的数据。这是我到目前为止所拥有的。我通常不是程序员,所以这是VisualStudio向导和google结果拼凑在一起的结果。 谢谢你的帮助 <form id="form1" runat="server"> <div> <a

因此,我在gridview中显示来自SQL表的结果。有些字段是电话号码。特定字段可以是普通的10位数字,但也可以是4位扩展名。如果它是一个4位数的数字,我想至少不要在上面加上10位数的格式,最多我想在它前面加上Ext:,然后加上我的数据。这是我到目前为止所拥有的。我通常不是程序员,所以这是VisualStudio向导和google结果拼凑在一起的结果。 谢谢你的帮助

    <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>

您需要使用
RowDataBound
事件拦截绑定到网格的每一行,以便确定电话号码是10位还是4位,并根据具体情况处理每个值,如下所示:

标记:

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

感谢您的帮助,我基本上理解了正在发生的事情,但在编译时遇到了两个相同的错误:“System.Web.UI.WebControl.GridViewRow.DataItem”不能像方法一样使用,这是指这一行:string newnumberValue=e.Row.DataItem(“NewNumber”).ToString();这个:label.Text=String.Format(“{0:(##########-#########),Convert.ToInt64(e.Row.DataItem(“NewNumber”);有什么想法吗?改为使用括号,如下:
e.Row.DataItem[“NewNumber”].ToString(),见更新的答案。我修正了它,不知道这是否是必须的,但增加了这一行:DataRowView rowView=(DataRowView)e.Row.DataItem;并将e.Row.DataItem(“NewNumber”)更改为rowView[“NewNumber”],它可以完美地工作。再次感谢!我也注意到了你的评论,我试过括号,但没有success@mloraditch-只是建议将其转换为
DataRowView
,很抱歉退出该步骤,我已经有一段时间没有执行您尝试执行的操作了。我将用正确的语法更新答案。
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;
            }
        }
    }
}