views:

346

answers:

2

I have a form with a date value in a TextBox control. The form uses data binding with a BindingSource against a DataSet and a SQL 2005 CE database. Where do I control the formatting of the date? Nowhere in the properties along the way did I see a possibility to strip out the time part, for instance.

I could of course do it in the database and pass a string instead of a DateTime, but that's workaround and not a solution.

+2  A: 

You can handle the event Binding.Format event to format the date. And its counterpart Binding.Parse to parse the input from the TextBox.

E.g.

TextBox.DataBindings["Text"].Format += new ConvertEventHandler(FormatDateEventHandler);
...
private void FormatDateEventHandler (object sender, ConvertEventArgs e)
{
    if (! Convert.IsDBNull (e.Value))
    {
     e.Value = ((DateTime)e.Value).ToString ("d", CultureInfo.CurrentCulture);
    }
}
Joe
+2  A: 

Using the designer, select your textbox, click on the properties tab, choose (DataBindings) - Advanced.

In here you can select the Date Time datatype and optionally strip out just the date or the time etc.

Ciaran
Wow, how did I not see that ... ?
cdonner