nf91
nf91

Reputation: 47

c# winform bindingsource format output string to currency without the $ symbol

I know this question have been asked many times but I still can't get mine to work. I am fairly unfamiliar with windows form so please bear with me.

I am trying to bind my data to a label in datarepeater using bindingsource. I wanted to display a string into currency without the $ sign (e.g. from 578288 to 578,288.00).

I have tried the code below:

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");
}

but it still output my string as 578288. I step the debugger into the function and it correctly format my string into currency ($578,288.00, not exactly what i want but at least something) but it doesn't get displayed in the label.

I also tried this:

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

but I can't get the comma to work since not every string have the same length (not exactly the right way too, i know).

Can somebody point to me if there's anything wrong with these codes and ways to correct it? Or any workaround?

Got the output that I want using this:

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

I just changed the string format from 0.00 to #,#.00

Should've known better.

Upvotes: 2

Views: 3524

Answers (2)

Vidal Lp
Vidal Lp

Reputation: 21

I don't know if my answer is still useful. You can try with this code, at least it worked for me.

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

Output:

$ 692,235.23`

Upvotes: 2

Fabio
Fabio

Reputation: 32445

You need thousand separator in the number format #,0.00

Custom Numeric Format Strings

Upvotes: 3

Related Questions