定义NumberFormatInfo在DataFormatString
-
12-09-2019 - |
题
我有一个ASP.NET 内有BoundField定小数值。我用以下代码格式的数货币的价值:
DataFormatString="{0:C}"
我们有一个自定义的执行情况的NumberFormatInfo虽然这消除了货币符号和修改的成千上万分离功能的.通常这种格式应用于诸如:
myDecimal.ToString("C", myCustomNFI);
我怎么指定一个定义NumberFormatInfo在BoundField元件的内?
感谢
解决方案
这可以通过使用定制定的领域。开始一个自定义BoundField类。下面我试图按照你的命名约定。
namespace CustomBoundField
{
public class NFIBoundField : System.Web.UI.WebControls.BoundField
{
protected override string FormatDataValue(object dataValue, bool encode)
{
if (dataValue == null || dataValue == System.DBNull.Value)
return "";
if (base.DataFormatString == string.Empty)
return dataValue.ToString();
// Format as you wish based on dataValue and DataFormatString argument
return string.Format("{0}", dataValue);
}
}
}
登记册在控制你的.ASPX文件:
<%@ Register Namespace="CustomBoundField" TagPrefix="custom" %>
基准的定义BoundField在你的内:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<custom:NFIBoundField DataField="Price" HeaderText="Price" DataFormatString="{0:NFI}"/>
</Columns>
</asp:GridView>
你将要播放的内的FormatDataValue()为了得到格式化你想要的。
一对夫妇的评论:
如果你想要你的定义BoundField到 处理多种格式,分析 基础。DataFormatString得到的 格式化的类型。换句话说, 提供{0:罪案}或者{0:NFI2}在 代码前可能导致不同 格式,如果你适应这个内FormatDataValue.
你可能想要考虑创建 你自己的格式提供,而不是 把所有你的格式的逻辑 右内的FormatDataValue 功能。
这种方法应该为你工作得很好。最好的运气。
其他提示
与尝试的 field.DataFormatString = CURRENCYSYMBOL + “{0:#,###。##}”; 强>
它工作正常的我。
我不是100%肯定,但我不认为你可以。
您将需要使用模板领域和绑定到文字上的RowDataBound而不是使用自定义格式。高兴被证明是错误的但是....
不隶属于 StackOverflow