Reputation: 37708
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.
Upvotes: 2
Views: 3150
Reputation: 2634
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.
Upvotes: 6
Reputation: 124804
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);
}
}
Upvotes: 7