c#winform bindingsource将输出字符串格式化为不带$符号的货币

时间:2015-10-28 03:04:58

标签: c# winforms string-formatting bindingsource

我知道这个问题已被多次询问,但我仍然无法让我的工作。我对Windows窗体相当不熟悉所以请耐心等待。

我正在尝试使用bindingsource将数据绑定到datarepeater中的标签。我想在没有$符号的情况下将字符串显示为货币(例如从578288到578,288.00)。

我尝试过以下代码:

Binding b = new Binding("Text", BSource, "PaidAmt");
b.Format += new ConvertEventHandler(DecimalToCurrency);
lblPaidAmount.DataBindings.Add(b);

private void DecimalToCurrency(object sender, ConvertEventArgs cevent)
{
    if (cevent.DesiredType != typeof(string)) return;

    cevent.Value = ((decimal)cevent.Value).ToString("c");
}

但它仍然输出我的字符串为578288.我将调试器输入函数并正确地将我的字符串格式化为货币($ 578,288.00,不完全是我想要的但至少是某些东西)但它没有显示在标签中

我也试过这个:

lblPaidAmount.DataBindings.Add("Text", BSource, "PaidAmt", true, DataSourceUpdateMode.Never, "", "0.00");

但我不能让逗号工作,因为不是每个字符串都有相同的长度(不完全正确的方式,我知道)。

如果这些代码有任何问题以及纠正方法,有人可以指点我吗?或者任何解决方法?

使用此输出得到我想要的输出:

lblPaidAmount.DataBindings.Add("Text", BSource, "AD1008", true, DataSourceUpdateMode.Never, "", "#,#.00");

我刚刚将字符串格式从0.00更改为#,#。00

应该知道的更好。

2 个答案:

答案 0 :(得分:1)

您需要数字格式为#,0.00

的千位分隔符

Custom Numeric Format Strings

答案 1 :(得分:0)

不知道我的回答还有没有用。 你可以试试这个代码,至少它对我有用。

txtSalary.DataBindings.Add(new Binding("text", Bs, "Salary"));
txtSalary.DataBindings[0].FormattingEnabled = true;
txtSalary.DataBindings[0].FormatString = "C2";

输出:

<块引用>

$ 692,235.23`