2

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.

nf91
  • 47
  • 2
  • 7
  • Can you explain more why `lblPaidAmount.DataBindings.Add("Text", BSource, "PaidAmt", true, DataSourceUpdateMode.Never, "", "0.00");` not working? – Fabio Oct 28 '15 at 03:24
  • @Fabio it only output 578288.00. I want to have commas as well (578,288.00) – nf91 Oct 28 '15 at 05:31

2 Answers2

3

You need thousand separator in the number format #,0.00

Custom Numeric Format Strings

Fabio
  • 31,528
  • 4
  • 33
  • 72
  • wow thanks! I used it like this to anyone looking for the answer: lblPaidAmount.DataBindings.Add("Text", BSource, "PaidAmt", true, DataSourceUpdateMode.Never, "", "#,#.00"); – nf91 Oct 28 '15 at 05:53
1

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`

juagicre
  • 1,065
  • 30
  • 42
Vidal Lp
  • 11
  • 2
  • Welcome to SO. Check out [the editor guide](https://stackoverflow.com/editing-help#code); it will help you better format your code here in your posts. – Connor Low Mar 09 '21 at 16:23