本文介绍了空值设置为Datetimepicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public partial class mst_item : Form
{
SqlConnection cn = null;
int originalExStyle = -1;
bool enableFormLevelDoubleBuffering = true;
void dtBirthdayNullable_Format(object sender, ConvertEventArgs e)
{
// e.Value is the object value, we format it to be what we want to show up in the control
Binding b = sender as Binding;
if (b != null)
{
dtBirthdayNullable = (b.Control as DateTimePicker);
if (dtBirthdayNullable != null)
{
if (e.Value == null)
{
dtBirthdayNullable.ShowCheckBox = true;
dtBirthdayNullable.Checked = false;
e.Value = dtBirthdayNullable.Value;
}
else
{
dtBirthdayNullable.ShowCheckBox = true;
dtBirthdayNullable.Checked = true;
// leave e.Value unchanged – it’s not null, so the DTP is fine with it.
}
}
}
}
//Binding v = new Binding("Value");
//Binding b = new Binding( "Value", person, "BdayNullable", true );
// dtBirthdayNullable.DataBindings.Add( b );
// b.Format += new ConvertEventHandler( dtBirthdayNullable_Format );
void dtBirthdayNullable_Parse(object sender, ConvertEventArgs e)
{
// e.value is the formatted value coming from the control.
// we change it to be the value we want to stuff in the object.
Binding b = sender as Binding;
if (b != null)
{
dtBirthdayNullable = (b.Control as DateTimePicker);
if (dtBirthdayNullable != null)
{
if (dtBirthdayNullable.Checked == false)
{
dtBirthdayNullable.ShowCheckBox = true;
dtBirthdayNullable.Checked = false;
System.Nullable(e.Value);
e.Value = new Nullable() ;
}
else
{
DateTime val = Convert.ToDateTime(e.Value);
e.Value = new Nullable();
}
}
}
}
private void mst_item_Load(object sender, EventArgs e)
{
try
{
cn.Open();
SqlCommand cmd = new SqlCommand("Select date1 from datetime", cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "datetime");
Binding b = new Binding("Value", ds, "datetime.date1", true);
dtBirthdayNullable.DataBindings.Add(b);
b.Format += new ConvertEventHandler(dtBirthdayNullable_Format);
b.Parse += new ConvertEventHandler(dtBirthdayNullable_Parse);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
但给出一个错误:无法创建静态类system.Nullable
的实例任何解决方案...
But give an error : Cannot create an instance of static class system.Nullable
any solution...
推荐答案
e.Value = null ;
希望对您有所帮助.
Hope it helps.
这篇关于空值设置为Datetimepicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!