Decimal Places on Text Boxes VS C# 2008

Apparently formats that work for data grid views do not work for text boxes. The fields I have at the bottom of this screen are masked text boxes. All I want is the number field to be displayed w/o any decimal places, the two numeric fields to have two decimal places and the percent number to be displayed as 999.99%. What is the correct way to accomplish this? Are Masked Text boxes correct or will a text box do? Should I do the conversion in the SQL select statement? The bottom numbers are sums or averages of what is displayed in the data grid view.
GPDataSet = new System.Data.DataSet();
                GPDataSet.CaseSensitive = false;

                GPCommand = new System.Data.SqlClient.SqlCommand();
                GPCommand.Connection = GPConnection;

                GPCommand.CommandText = "select sum(convert(int, sol.quantity)) AS 'TotalQTY',sum(sol.xtndprce) AS 'TotalPrice',sum(CASE WHEN pop.ponumber is NULL THEN sol.unitcost * sol.quantity ELSE pop.unitcost * sol.quantity END) as 'TotalCost',avg(CASE WHEN pop.unitcost* sol.quantity = 0 or pop.ponumber is NULL THEN CASE WHEN sol.extdcost = 0 THEN 0 ELSE (sol.xtndprce-sol.extdcost)/sol.extdcost * 100.00 END ELSE (sol.xtndprce-(pop.unitcost * sol.quantity))/(pop.unitcost * sol.quantity)  * 100.00 END) as 'AvgMargin' from sop10200 sol LEFT OUTER JOIN SOP60100 sop on sol.sopnumbe = sop.sopnumbe and sol.lnitmseq = sop.lnitmseq LEFT OUTER JOIN POP10110 pop on sop.ponumber = pop.ponumber and sop.ord = pop.ord WHERE SOL.SOPNUMBE = '" + sopEntryWindow.SopNumber.Value + "'"; 
                GPDataAdapter = new System.Data.SqlClient.SqlDataAdapter();
                GPDataAdapter.SelectCommand = GPCommand;
                GPDataAdapter.TableMappings.Add("Table", "OrderSummary");


                this.txtTotalQuantity.DataBindings.Add("Text", GPDataSet.Tables["OrderSummary"], "TotalQTY").ToString();
                this.txtTotalQuantity.Text = Convert.ToString("N0");
                this.txtTotalPrice.DataBindings.Add("Text", GPDataSet.Tables["OrderSummary"], "TotalPrice").ToString();
                this.txtTotalPrice.Text = Convert.ToString("N2");
                this.txtTotalCost.DataBindings.Add("Text", GPDataSet.Tables["OrderSummary"], "TotalCost").ToString();
                this.txtTotalCost.Text = Convert.ToString("N2");
                this.txtAvgMargin.DataBindings.Add("Text", GPDataSet.Tables["OrderSummary"], "AvgMargin").ToString();
                this.txtAvgMargin.Text = Convert.ToString("N2");

Open in new window

Who is Participating?
cyberkiwiConnect With a Mentor Commented:
Ignore that, I just tried it and I see your point about the ___ which is what you are trying to get rid of.  Check out this link for a very short bit of code that handles free-form input and always-formatted display on exit.
The Textbox control does not allow a format mask, you can use key and change events to keep formatting it, but easier to just use MaskedTextBox.
rwheeler23Author Commented:
Yes, but using the MaskedTextBox in the Mask box if I want to have a currency field that can be up to 7 digits to the left of decimal and 2 digits to the right and I do NOT want to see the underline character plus have the dollar sign to the left, what mask do I type?

I tried $99,999,999.99, $00,000,000.00, and $##,###,###.## but it keep putting the underline character. I jsut want to be $1,234,567.89 or $29.99.
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Set the .Mask property to ">LCCC 9LL"
rwheeler23Author Commented:
This code has this command in it. What context must I include to get it to recognize Globalization?
If Not Decimal.TryParse(Me.TextBox1.Text, _
                            Globalization.NumberStyles.Currency, _
                            Nothing, _
rwheeler23Author Commented:
I tried using this but it does not recognize NumberStyles and of course Currency
This is VS 2008 C#
using System.Globalization.NumberStyles.Currency
rwheeler23Author Commented:
I have typing in the entire context and it does not like this line:

 if (!decimal.TryParse(this.txtTotalPrice.Text, System.Globalization.NumberStyles.Currency , null, currency))
It jsut does understand this line.
private void txtTotalPrice_Validating(object sender, CancelEventArgs e)
            decimal currency = default(decimal);
            //Convert the current value to currency, with or without a currency symbol.
            if (!decimal.TryParse(this.txtTotalPrice.Text, System.Globalization.NumberStyles.Currency.ToString , null, currency))
            /*Don't let the user leave the field if the value is invalid. */
                this.txtTotalPrice.HideSelection = false;
                MessageBox.Show("Please enter a valid currency amount.", "Invalid Value", MessageBoxButtons.OK, MessageBoxIcon.Error);

                this.txtTotalPrice.HideSelection = true;
            e.Cancel = true;

Open in new window

rwheeler23Author Commented:
Is there some reference I need to add to my project? If so, which one?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.