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); } }
e.Value = null ;
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)