5 Replies - 1311 Views - Last Post: 30 April 2012 - 03:30 PM Rate Topic: -----

#1 fullyunknown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 23-March 12

format textbox result from databinding

Posted 23 April 2012 - 12:32 PM

Hello....

I have a WinForm, and on this form I have 4 main things:
  • Textbox - for bill amount
  • BindingSource
  • BindingNavigator
  • DataSet


The following code sets the bindingsource; "datasource" and "datamember" to the currently stored dataset(this dataset was populated from a bill table in a access database, where it was formatted as currency):
bs = bindingsource created on the form.

bs.DataMember = "Bills";
bs.DataSource = dsMain.Tables["Bills"];



then I add a databinding to the textbox

totalTextBox.DataBindings.Add("Text", bs, "BillAmount");



this all works perfectly fine. However, the data is not being output as currency, but instead as number. I am trying to figure out how to make it output the results to the textbox as currency. Is the only way to format the datatable before binding and displaying the results?

Is This A Good Question/Topic? 0
  • +

Replies To: format textbox result from databinding

#2 Toadill  Icon User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 346
  • Joined: 08-January 12

Re: format textbox result from databinding

Posted 23 April 2012 - 12:40 PM

Okay when you read the file in you will need still need to convert the data type to decimal.

foreach(dataRow[] dr in dataset.tables[your table index])
 {
  // max. two decimal places
   string val =   String.Format("{0:0.##}",Convert.ToDecimal(dr[column index])); 
   dr[column index] = val;
 }




Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 1948
  • View blog
  • Posts: 8,665
  • Joined: 29-May 08

Re: format textbox result from databinding

Posted 23 April 2012 - 12:47 PM

MSDN Control.DataBindings
The link to the documentation gives you a good example.
Was This Post Helpful? 1
  • +
  • -

#4 fullyunknown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 23-March 12

Re: format textbox result from databinding

Posted 23 April 2012 - 01:28 PM

Thank you both for your help....

for anyone that finds this posting later. if you visit the mSDN site they will give you an example like:

   Binding b = new Binding("Text", ds, "customers.custToOrders.OrderAmount");
   b.Format += new ConvertEventHandler(DecimalToCurrency);
   textBox3.DataBindings.Add(B)/>;



have to remember the "DecimalToCurrencyString" is not a built in method, type, fuction, etc.. you will need to create it yourself. I found another of their examples which contained the method:

private void DecimalToCurrency(object sender, ConvertEventArgs cevent)
{
   // The method converts only to string type. Test this using the DesiredType.
   if (cevent.DesiredType != typeof(string)) return;

   // Use the ToString method to format the value as currency ("c").
   cevent.Value = ((decimal) cevent.Value).ToString("c");
}



with that it will convert each data binding you assign to the currency. However, it does not convert all results at once. When you are navigating through the results, selecting new rows, that is when it converts it.

for my problem i might have to go with formating the DataTable in the DataSet. Since the DataSet is used through the whole program and do not want to have to convert the amount to decimal on every single form that may need that field.

Once again thanks for all your help...
Was This Post Helpful? 0
  • +
  • -

#5 fullyunknown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 23-March 12

Re: format textbox result from databinding

Posted 30 April 2012 - 03:20 PM

Hello, a followup question to my previous answer.

The code below works perfectly for formating my textboxes to currency but kind of ran into another problem.
private void DecimalToCurrency(object sender, ConvertEventArgs cevent)
        {
            // The method converts only to string type. Test this using the DesiredType.
            if (cevent.DesiredType != typeof(string)) return;

            // verify value is not null
            try
            {
                // Use the ToString method to format the value as currency ("c").
                cevent.Value = ((decimal)cevent.Value).ToString("c");
            }
            catch
            {
                return;
            }
        }



I want to add the ability when the user changes the value in one of the textboxes it sums all the textboxes together and subtracts that value from the bills balance. However, I am getting a error for converting the textboxes to decmial to do the math, because i assume its in currency format.

decimal cc = Convert.ToDecimal(ccTextBox.Text);

string s = ccTextBox.Text.ToString();
decimal cc = Convert.ToDecimal(s);



I have tried both ways in the above code but get the error: "Input string was not in a correct format"

So is the only way i can pull the values to do math, is to never convert the textbox values to currency in the first place?
Was This Post Helpful? 0
  • +
  • -

#6 fullyunknown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 23-March 12

Re: format textbox result from databinding

Posted 30 April 2012 - 03:30 PM

Never-mind. Finally googled the correct keywords.

Found out i have to do as such:
decimal cc = decimal.Parse(ccTextBox.Text, System.Globalization.NumberStyles.Currency);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1