468,252 Members | 1,468 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,252 developers. It's quick & easy.

Nearly Finished Null Enabled DateTimePicker

Hi Everyone,

There are so many Nullable DateTimePickers out there that this post may not even be found among all the others, but I wanted to try. First, I want to thank all the authors of the code I used as a starting point. Second, I hope this code helps someone as much as they helped me.

I have built a Nullable DateTimePicker and NullableCalendarColumn/Cell/EditingControl that is nearly 100% complete, as far as I can tell. There is only one remaining issue and it has me stalled.

When the user clicks into the cell containing the Nullable control (pleaced in a DataGridView), the first time, and only the first time, instead of the date already there, today's date appears. As an example, say that the cell contained 1/1/08, the DataGridView has just been created, and the user clicks (twice) in the cell to enable the EditingControl. Instead of 1/1/08, the date that appears is 7/20/08. Again, this happens only the very first time the control is used. After that it works as it should, initializing the control to the date in the cell.

Any thoughts?

Todd

P.S. Anyone that wants to expand on this or make disparaging comments about my code, feel free ... I can use all the help I can get!

************************************************** ************
NullableDateTimePicker.cs
************************************************** ************
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Windows.Forms;
  7.  
  8. namespace Custom
  9. {
  10.     public class NullableDateTimePicker : System.Windows.Forms.DateTimePicker
  11.     {
  12.         private Boolean isNull = false;
  13.         private DateTimePickerFormat baseFormat = DateTimePickerFormat.Short;
  14.         private Boolean ignoreBindOnFormat = false;
  15.  
  16.         public NullableDateTimePicker()
  17.         {
  18.             this.Format = baseFormat;
  19.             if (baseFormat == DateTimePickerFormat.Custom) this.CustomFormat = " ";
  20.         }
  21.  
  22.         public Boolean IsNull
  23.         {
  24.             get { return isNull; }
  25.             set
  26.             {
  27.                 isNull = value;
  28.                 this.Checked = value;
  29.             }
  30.         }
  31.  
  32.         //TODO: Add BaseCustomFormat
  33.  
  34.         public DateTimePickerFormat BaseFormat
  35.         {
  36.             get { return baseFormat; }
  37.             set { baseFormat = value; }
  38.         }
  39.  
  40.         public object BindMe
  41.         {
  42.             get
  43.             {
  44.                 if (IsNull) return System.DBNull.Value;
  45.                 else return base.Value;
  46.             }
  47.             set
  48.             {
  49.                 String s = this.Name;
  50.  
  51.                 if (ignoreBindOnFormat) return;
  52.  
  53.                 if (System.Convert.IsDBNull(value))
  54.                 {
  55.                     // for some reason setting base.format in this.format calls set BindMe.
  56.                     // we need to ignore the following call
  57.                     ignoreBindOnFormat = true;
  58.                     this.Format = DateTimePickerFormat.Custom;
  59.                     this.CustomFormat = " ";
  60.                     IsNull = true;
  61.                     ignoreBindOnFormat = false;
  62.                 }
  63.                 else
  64.                 {
  65.                     this.Format = baseFormat;
  66.                     if (baseFormat == DateTimePickerFormat.Custom) this.CustomFormat = " ";
  67.                     IsNull = false;
  68.                     base.Value = (DateTime)value;
  69.                 }
  70.             }
  71.         }
  72.  
  73.         protected override void OnKeyDown(KeyEventArgs e)
  74.         {
  75.             base.OnKeyDown(e);
  76.  
  77.             if (e.KeyCode == Keys.Delete)
  78.             {
  79.                 this.BindMe = DBNull.Value;
  80.             }
  81.         }
  82.  
  83.         protected override void OnCloseUp(EventArgs eventargs)
  84.         {
  85.             base.OnCloseUp(eventargs);
  86.             BindMe = base.Value;
  87.         }
  88.     }
  89. }
  90.  
  91.  
************************************************** ************
NullableDateTimePicker.cs
************************************************** ************
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5.  
  6.  
  7. using System.Drawing;
  8.  
  9. namespace Custom
  10. {
  11.  
  12.     public class NullableCalendarColumn : DataGridViewColumn
  13.     {
  14.  
  15.         public NullableCalendarColumn() : base(new NullableCalendarCell()) { }
  16.  
  17.         public NullableCalendarColumn(string frm) : base(new NullableCalendarCell(frm)) { }
  18.  
  19.         public void setValue(string frm)
  20.         {
  21.             base.CellTemplate.Value = frm;
  22.         }
  23.  
  24.         public override DataGridViewCell CellTemplate
  25.         {
  26.             get { return base.CellTemplate; }
  27.             set
  28.             {
  29.                 // Ensure that the cell used for the template is a CalendarCell.
  30.                 if (value != null && !value.GetType().IsAssignableFrom(typeof(NullableCalendarCell)))
  31.                 {
  32.                     throw new InvalidCastException("Must be a NullableCalendarCell");
  33.                 }
  34.  
  35.                 base.CellTemplate = value;
  36.             }
  37.         }
  38.     }
  39.  
  40.     public class NullableCalendarCell : DataGridViewTextBoxCell
  41.     {
  42.         private String baseFormat = "MM/dd/yyyy";
  43.  
  44.         public NullableCalendarCell()
  45.             : base()
  46.         {
  47.             // Use the short date format.
  48.             // Change the format here for the normal display.
  49.             this.Style.Format = baseFormat;
  50.         }
  51.  
  52.         public NullableCalendarCell(String frm)
  53.             : base()
  54.         {
  55.             baseFormat = frm;
  56.             this.Style.Format = frm;
  57.         }
  58.  
  59.         public String BaseFormat
  60.         {
  61.             get { return baseFormat; }
  62.             set { baseFormat = value; }
  63.         }
  64.  
  65.         public override void InitializeEditingControl(int rowIndex,
  66.             object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
  67.         {
  68.             // Set the value of the editing control to the current cell value.
  69.             base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
  70.  
  71.             NullableCalendarEditingControl ctl = DataGridView.EditingControl as NullableCalendarEditingControl;
  72.  
  73.             if (this.Value.ToString() == "")
  74.             {
  75.                 ctl.BindMe = DBNull.Value;
  76.             }
  77.             else
  78.             {
  79.                 ctl.Value = (DateTime)this.Value;
  80.                 ctl.Text = this.Value.ToString();
  81.                 ctl.EditingControlFormattedValue = this.Value.ToString();
  82.                 ctl.IsNull = false;
  83.                 ctl.BindMe = (DateTime)this.Value;
  84.                 ctl.Left = 100;
  85.                 ctl.Name = "toddsctl";
  86.             }
  87.         }
  88.  
  89.  
  90.         public override Type EditType
  91.         {
  92.             get
  93.             {
  94.                 // Return the type of the editing contol that CalendarCell uses.
  95.                 return typeof(NullableCalendarEditingControl);
  96.             }
  97.         }
  98.  
  99.         public override Type ValueType
  100.         {
  101.             get
  102.             {
  103.                 // Return the type of the value that CalendarCell contains.
  104.                 return typeof(String);
  105.             }
  106.         }
  107.  
  108.         public override object DefaultNewRowValue
  109.         {
  110.             get
  111.             {
  112.                 // Use the current date and time as the default value.
  113.                 return DateTime.Now.ToString();
  114.             }
  115.         }
  116.  
  117.         public override void DetachEditingControl()
  118.         {
  119.             base.DetachEditingControl();
  120.  
  121.             if (this.EditedFormattedValue.ToString().Length == 0)
  122.             {
  123.                 this.Value = DBNull.Value;
  124.             }
  125.         }
  126.  
  127.         protected override void OnKeyDown(KeyEventArgs e, int rowIndex)
  128.         {
  129.             base.OnKeyDown(e, rowIndex);
  130.  
  131.             if (e.KeyCode == Keys.Delete) this.Value = DBNull.Value;
  132.         }
  133.     }
  134.  
  135.     class NullableCalendarEditingControl : NullableDateTimePicker, IDataGridViewEditingControl
  136.     {
  137.         DataGridView dataGridView;
  138.         private bool valueChanged = false;
  139.         int rowIndex;
  140.  
  141.         public NullableCalendarEditingControl()
  142.         {
  143.             // change the customformat here for how it shows when editing
  144.             this.Format = this.BaseFormat;
  145.         }
  146.  
  147.         // Implements the IDataGridViewEditingControl.EditingControlFormattedValue
  148.         // property.
  149.         public object EditingControlFormattedValue
  150.         {
  151.             get { return this.BindMe.ToString(); }
  152.             set
  153.             {
  154.                 if (value == null || value as String == "") this.BindMe = DBNull.Value;
  155.                 else
  156.                 {
  157.                     this.BindMe = DateTime.Parse((String)value);
  158.                     //this.Value = DateTime.Parse((String)value);
  159.                 }
  160.             }
  161.         }
  162.  
  163.         // Implements the
  164.         // IDataGridViewEditingControl.GetEditingControlFormattedValue method.
  165.         public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
  166.         {
  167.             return EditingControlFormattedValue;
  168.         }
  169.  
  170.         // Implements the
  171.         // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
  172.         public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
  173.         {
  174.             this.Font = dataGridViewCellStyle.Font;
  175.             this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
  176.             this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
  177.         }
  178.  
  179.         // Implements the IDataGridViewEditingControl.EditingControlRowIndex property.
  180.         public int EditingControlRowIndex
  181.         {
  182.             get { return rowIndex; }
  183.             set { rowIndex = value; }
  184.         }
  185.  
  186.         // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey method.
  187.         public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
  188.         {
  189.             // Let the DateTimePicker handle the keys listed.
  190.             switch (key & Keys.KeyCode)
  191.             {
  192.                 case Keys.Left:
  193.                 case Keys.Up:
  194.                 case Keys.Down:
  195.                 case Keys.Right:
  196.                 case Keys.Home:
  197.                 case Keys.End:
  198.                 case Keys.PageDown:
  199.                 case Keys.PageUp:
  200.                 case Keys.Tab:
  201.                 case Keys.Delete:
  202.                     return true;
  203.                 default:
  204.                     return false;
  205.             }
  206.         }
  207.  
  208.         // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit method.
  209.         public void PrepareEditingControlForEdit(bool selectAll)
  210.         {
  211.             // No preparation needs to be done.
  212.         }
  213.  
  214.         // Implements the IDataGridViewEditingControl.RepositionEditingControlOnValueChange property.
  215.         public bool RepositionEditingControlOnValueChange
  216.         {
  217.             get { return false; }
  218.         }
  219.  
  220.         // Implements the IDataGridViewEditingControl.EditingControlDataGridView property.
  221.         public DataGridView EditingControlDataGridView
  222.         {
  223.             get { return dataGridView; }
  224.             set { dataGridView = value; }
  225.         }
  226.  
  227.         // Implements the IDataGridViewEditingControl.EditingControlValueChanged property.
  228.         public bool EditingControlValueChanged
  229.         {
  230.             get { return valueChanged; }
  231.             set { valueChanged = value; }
  232.  
  233.         }
  234.  
  235.         // Implements the IDataGridViewEditingControl.EditingPanelCursor property.
  236.         public Cursor EditingPanelCursor
  237.         {
  238.             get { return base.Cursor; }
  239.         }
  240.  
  241.         protected override void OnValueChanged(EventArgs eventargs)
  242.         {
  243.             // Notify the DataGridView that the contents of the cell have changed.
  244.             valueChanged = true;
  245.             this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
  246.             base.OnValueChanged(eventargs);
  247.         }
  248.     }
  249. }
Jul 21 '08 #1
0 2324

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Dean L. Howen | last post: by
7 posts views Thread by JJ | last post: by
reply views Thread by Bernie Yaeger | last post: by
reply views Thread by Benjamin Bittner | last post: by
4 posts views Thread by jehugaleahsa | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.