Format bound data in a textbox

  • Thread starter Thread starter Michael Kellogg
  • Start date Start date
M

Michael Kellogg

I have a textbox on my Winform that is data bound to a floating-point
element. It's displaying the number out to like 9 digits. Since I'm not
"handing" the textbox the value using ".ToString("f3")" I'm at a loss for
how to control the formatting. Is there a simple way to accomplish this?
 
Try,
Binding binding = new Binding("Text", dataTable, "Column1");
binding.Format += new ConvertEventHandler(DoubleToString);
binding.Parse += new ConvertEventHandler(StringToDouble);
textBox1.DataBindings.Add(binding);
..
..
..
private void DoubleToString(object sender, ConvertEventArgs e)
{
if (e.DesiredType != typeof(string)) return;
e.Value = System.Convert.ToDouble(e.Value).ToString("f3");
}

private void StringToDouble(object sender, ConvertEventArgs e)
{
if (e.DesiredType != typeof(double)) return;
e.Value = double.Parse(e.Value.ToString());
}

Regards,
Phil.
 
Back
Top