#region String/Decimal Conversion private void SetTextBoxCurrencyBinding(TextBox txtbox, quote ds, string fieldname) { Binding b = new Binding("Text", ds, fieldname); { b.Format += new ConvertEventHandler(DecimalToCurrencyString); b.Parse += new ConvertEventHandler(CurrencyStringToDecimal); } txtbox.DataBindings.Clear(); txtbox.DataBindings.Add(b); } private void DecimalToCurrencyString(object sender, ConvertEventArgs cevent) { // The method converts only to string type.. if (cevent.DesiredType != typeof(string)) return; // Use the ToString method to format the value as currency ("c"). cevent.Value = ((decimal)cevent.Value).ToString("N2"); } private void CurrencyStringToDecimal(object sender, ConvertEventArgs cevent) { // The method converts back to decimal type only. if (cevent.DesiredType != typeof(decimal)) return; // Converts the string back to decimal using the static Parse method. cevent.Value = Decimal.Parse(cevent.Value.ToString(), NumberStyles.Currency, null); } private void CurrencyStringToDollars(object sender, ConvertEventArgs cevent) { // The method converts back to decimal type only. if (cevent.DesiredType != typeof(Int32)) return; // Converts the string back to Integer using the static Parse method. cevent.Value = Int32.Parse(cevent.Value.ToString(), NumberStyles.Currency, null); } private void SetTextBoxDollarsBinding(TextBox txtbox, DataSet ds, string fieldname) { Binding b = new Binding("Text", ds, fieldname); { b.Format += new ConvertEventHandler(DecimalToDollarsString); b.Parse += new ConvertEventHandler(CurrencyStringToDecimal); } txtbox.DataBindings.Clear(); txtbox.DataBindings.Add(b); } private void DecimalToDollarsString(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("N0"); } #endregion