c# – Gridview格式字段为电话号码,但有些结果是4位数的扩展名,如何处理?

c# – Gridview格式字段为电话号码,但有些结果是4位数的扩展名,如何处理?,第1张

概述所以我在gridview中显示SQL表的结果.一些字段是电话号码.特定字段可以是正常的10位数字,但也可以是4位数字的扩展名.如果它是一个4位数字,我想至少不要在其上放置10位数字格式,最多我想在它前面加上Ext:然后是我的数据.这是我到目前为止所拥有的.我通常不是程序员,所以这是Visual Studio向导和谷歌结果拼凑在一起. 非常感谢您的帮助. <form id="form1" runat 所以我在grIDvIEw中显示sql表的结果.一些字段是电话号码.特定字段可以是正常的10位数字,但也可以是4位数字的扩展名.如果它是一个4位数字,我想至少不要在其上放置10位数字格式,最多我想在它前面加上Ext:然后是我的数据.这是我到目前为止所拥有的.我通常不是程序员,所以这是Visual Studio向导和谷歌结果拼凑在一起.
非常感谢您的帮助.

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

注意:删除Text =’<%#String.Format(“{0:(###)### - ####}”,Convert.ToInt64(DataBinder.Eval(Container.DataItem,“CallDestination”) )))%>”来自< asp:Label>在< ItemTemplate>中,因为您将格式化文本并在RowDataBound事件中设置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;            }        }    }}
总结

以上是内存溢出为你收集整理的c# – Gridview格式字段为电话号码,但有些结果是4位数的扩展名,如何处理?全部内容,希望文章能够帮你解决c# – Gridview格式字段为电话号码,但有些结果是4位数的扩展名,如何处理?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1230109.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存