DataTextFormatString下拉列表
本文关键字:下拉列表 DataTextFormatString | 更新日期: 2023-09-27 18:26:55
我在使用DataTextFormatString属性时遇到问题,当值绑定到下拉列表时,实际上并没有对其进行格式化。
dvWaterLimits = this._lookupProvider.GetDataTable(version, form, package, state, DateTime.Today(), Zone);
this.ddlMyDropDown.DataSource = dvWaterLimits;
this.ddlMyDropDown.DataTextField = "LimitDescription";
this.ddlMyDropDown.DataValueField = "TotalLimit";
this.ddlMyDropDown.DataTextFormatString = " {0:$###,###,##0} ";
this.ddlMyDropDown.DataBind();
我正在返回的数据表如下所示:其中第二列为TotalLimit
,第五列为LimitDescription
1133 5000 2 3 $5000
1133 10000 2 3 $10000
1133 15000 2 3 $15000
1133 20000 2 3 $20000
1133 25000 2 3 $25000
下拉列表看起来就像第五列,而它应该看起来像$5000$10000……等等,带逗号。
在格式字符串中使用C。
{0:C}
C或C以货币格式显示数值。您可以指定小数位数。
注意:逗号在美国不用于小数!
一个选项可以使用克隆的en-US
区域性来覆盖此操作的CurrentCulture
,该区域性将CurrencyDecimalDigits
设置为0
,并将DataTextFormatString
属性的"C"
格式说明符用作"{0:C}"
。
不清楚您想要的是$5,000
还是$5000
。如果您不需要组分隔符,则还需要将CurrencyGroupSeparator
分配给空字符串。
作为一个例子;
int i = 5000;
var clone = (CultureInfo)CultureInfo.GetCultureInfo("en-US").Clone();
clone.NumberFormat.CurrencyDecimalDigits = 0;
Console.WriteLine(i.ToString("C", clone)); // $5,000
或
int i = 5000;
var clone = (CultureInfo)CultureInfo.GetCultureInfo("en-US").Clone();
clone.NumberFormat.CurrencyDecimalDigits = 0;
clone.NumberFormat.CurrencyGroupSeparator = "";
Console.WriteLine(i.ToString("C", clone)); // $5000
或者在不设置CurrencyDecimalDigits
属性的情况下更好;
int i = 5000;
Console.WriteLine(i.ToString("C0", CultureInfo.GetCultureInfo("en-US"))); // $5,000
老实说,我不知道如何覆盖的文化,在asp.net中只有一个元素(或者可能与否),但你可以检查这些主题;
- 有没有一种方法可以为整个应用程序设置文化?所有当前线程和新线程
- 如何:为ASP.NET网页全球化设置区域性和UI区域性
所以不确定它是'$',还是值是以字符串的形式输入的,所以它无法在#和输入的值之间进行匹配。但在我做出这个无效的更改后,这就开始起作用了。
foreach (DataRow dr in dtClone.Rows)
{
if (dr["LimitDescription"].ToString().Contains("$"))
{
dr["LimitDescription"] = Convert.ToDouble(dr["LimitDescription"].ToString().Replace("$", "")).ToString("$###,###,##0");
}
}