cdo*_*ner 2 data-binding formatting datetime compact-framework winforms
我在TextBox控件中有一个带有日期值的表单.该表单使用数据绑定与BindingSource对DataSet和SQL 2005 CE数据库.我在哪里控制日期的格式?例如,在沿途的物业中,我没有看到剥离时间部分的可能性.
我当然可以在数据库中执行它并传递字符串而不是DateTime,但这是解决方法而不是解决方案.
您可以处理事件Binding.Format事件以格式化日期.和它的对应Binding.Parse来解析来自TextBox的输入.
例如
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);
}
}
Run Code Online (Sandbox Code Playgroud)