Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 4.03 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Creating a nullable Datagridview DateTime Column
  2. public class CtlDataGridViewNullableCalendarCell : DataGridViewTextBoxCell
  3. {
  4.  
  5. public CtlDataGridViewNullableCalendarCell()
  6.     : base()
  7. {
  8.     this.Style.Format = "d";
  9. }
  10.  
  11. public override void InitializeEditingControl(int rowIndex, object
  12.     initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
  13. {
  14.     // Set the value of the editing control to the current cell value.
  15.     base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
  16.     CtlCalendarNullableEditingControl ctl = DataGridView.EditingControl as CtlCalendarNullableEditingControl;
  17.     ctl.Value = (DateTime?)this.Value;
  18.  
  19.     // a hacky way of getting the DateTimePicker to reset its focus, rather than remembering which date part was previously focussed
  20.     DateTimePickerFormat format = ctl.Format;
  21.     ctl.Format = DateTimePickerFormat.Custom;
  22.     ctl.Format = format;
  23. }
  24. public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
  25. {
  26.     if (formattedValue == null)
  27.         return null;
  28.     else
  29.         return (formattedValue as DateTime?).Value.Date;
  30. }
  31. public override Type EditType
  32. {
  33.     get
  34.     {
  35.         // Return the type of the editing control that CalendarCell uses.
  36.         return typeof(CtlCalendarNullableEditingControl);
  37.     }
  38. }
  39.  
  40. public override Type ValueType
  41. {
  42.     get
  43.     {
  44.         // Return the type of the value that CalendarCell contains.
  45.         return typeof(DateTime?);
  46.     }
  47. }
  48.  
  49. public override object DefaultNewRowValue
  50. {
  51.     get
  52.     {
  53.         // Use the current date as the default value.
  54.         return null;
  55.     }
  56. }
  57.  
  58. #endregion
  59. }
  60.        
  61. class CtlCalendarNullableEditingControl : CtlNullableDateTimePicker, IDataGridViewEditingControl
  62. {
  63. DataGridView _dataGridView;
  64. private bool _valueChanged = false;
  65. int _rowIndex;
  66.  
  67. public object EditingControlFormattedValue
  68. {
  69.     get
  70.     {
  71.         return this.Value;
  72.     }
  73.     set
  74.     {
  75.         if (value is string)
  76.             if ((string)value == string.Empty)
  77.                 value = null;
  78.             else
  79.                 value = DateTime.Parse((string)value);
  80.  
  81.     }
  82. }
  83.  
  84. public object GetEditingControlFormattedValue(
  85.     DataGridViewDataErrorContexts context)
  86. {
  87.     return EditingControlFormattedValue as DateTime?;
  88. }
  89.  
  90. public void ApplyCellStyleToEditingControl(
  91.     DataGridViewCellStyle dataGridViewCellStyle)
  92. {
  93.     this.Font = dataGridViewCellStyle.Font;
  94.     this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
  95.     this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
  96. }
  97.  
  98. public int EditingControlRowIndex
  99. {
  100.     get
  101.     {
  102.         return _rowIndex;
  103.     }
  104.     set
  105.     {
  106.         _rowIndex = value;
  107.     }
  108. }
  109.  
  110. public bool EditingControlWantsInputKey(
  111.     Keys key, bool dataGridViewWantsInputKey)
  112. {
  113.     // Let the DateTimePicker handle the keys listed.
  114.     switch (key & Keys.KeyCode)
  115.     {
  116.         case Keys.Left:
  117.         case Keys.Up:
  118.         case Keys.Down:
  119.         case Keys.Right:
  120.         case Keys.Home:
  121.         case Keys.End:
  122.         case Keys.PageDown:
  123.         case Keys.PageUp:
  124.         case Keys.Delete:
  125.         case Keys.Back:
  126.             return true;
  127.         default:
  128.             return false;
  129.     }
  130. }
  131.  
  132. public DataGridView EditingControlDataGridView
  133. {
  134.     get
  135.     {
  136.         return _dataGridView;
  137.     }
  138.     set
  139.     {
  140.         _dataGridView = value;
  141.     }
  142. }
  143.  
  144. public bool EditingControlValueChanged
  145. {
  146.     get
  147.     {
  148.         return _valueChanged;
  149.     }
  150.     set
  151.     {
  152.         _valueChanged = value;
  153.     }
  154. }
  155.  
  156.  
  157. #region -- DateTimePicker overrides --
  158.  
  159. /// <summary>
  160. /// Handle the OnValueChanged event from the <see cref="DateTimePicker"/> and ensure the change propagates to the grid.
  161. /// </summary>
  162. /// <param name="eventargs"></param>
  163. protected override void OnValueChanged(EventArgs eventargs)
  164. {
  165.     // Notify the DataGridView that the contents of the cell have changed.
  166.     _valueChanged = true;
  167.     this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
  168.     base.OnValueChanged(eventargs);
  169. }
  170.        
  171. if ((string)value == string.Empty)
  172.        
  173. if (formattedValue == null)