DataGridView控件内建立日期选择编辑列

时间:2023-12-02 22:45:50

两个文件:

CalendarColumn 类:

  1. public class CalendarColumn : DataGridViewColumn
  2. {
  3. public CalendarColumn()
  4. : base(new CalendarCell())
  5. {
  6. }
  7. public override DataGridViewCell CellTemplate
  8. {
  9. get
  10. {
  11. return base.CellTemplate;
  12. }
  13. set
  14. {
  15. // Ensure that the cell used for the template is a CalendarCell.
  16. if (value != null &&
  17. !value.GetType().IsAssignableFrom(typeof(CalendarCell)))
  18. {
  19. throw new InvalidCastException("Must be a CalendarCell");
  20. }
  21. base.CellTemplate = value;
  22. }
  23. }
  24. }

**********************************************************************

CalendarCell 类:

  1. public class CalendarCell : DataGridViewTextBoxCell
  2. {
  3. public CalendarCell()
  4. : base()
  5. {
  6. // Use the short date format.
  7. this.Style.Format = "d";
  8. }
  9. public override void InitializeEditingControl(int rowIndex, object
  10. initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
  11. {
  12. // Set the value of the editing control to the current cell value.
  13. base.InitializeEditingControl(rowIndex, initialFormattedValue,
  14. dataGridViewCellStyle);
  15. CalendarEditingControl ctl =
  16. DataGridView.EditingControl as CalendarEditingControl;
  17. if (this.Value == null)
  18. ctl.Value = DateTime.Now;
  19. else
  20. ctl.Value = (DateTime)this.Value;
  21. }
  22. public override Type EditType
  23. {
  24. get
  25. {
  26. // Return the type of the editing contol that CalendarCell uses.
  27. return typeof(CalendarEditingControl);
  28. }
  29. }
  30. public override Type ValueType
  31. {
  32. get
  33. {
  34. // Return the type of the value that CalendarCell contains.
  35. return typeof(DateTime);
  36. }
  37. }
  38. public override object DefaultNewRowValue
  39. {
  40. get
  41. {
  42. // Use the current date and time as the default value.
  43. return DateTime.Now;
  44. }
  45. }
  46. }
  47. class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
  48. {
  49. DataGridView dataGridView;
  50. private bool valueChanged = false;
  51. int rowIndex;
  52. public CalendarEditingControl()
  53. {
  54. this.Format = DateTimePickerFormat.Short;
  55. }
  56. // Implements the IDataGridViewEditingControl.EditingControlFormattedValue
  57. // property.
  58. public object EditingControlFormattedValue
  59. {
  60. get
  61. {
  62. return this.Value.ToShortDateString();
  63. }
  64. set
  65. {
  66. String newValue = value as String;
  67. if (newValue != null)
  68. {
  69. this.Value = DateTime.Parse(newValue);
  70. }
  71. }
  72. }
  73. // Implements the
  74. // IDataGridViewEditingControl.GetEditingControlFormattedValue method.
  75. public object GetEditingControlFormattedValue(
  76. DataGridViewDataErrorContexts context)
  77. {
  78. return EditingControlFormattedValue;
  79. }
  80. // Implements the
  81. // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
  82. public void ApplyCellStyleToEditingControl(
  83. DataGridViewCellStyle dataGridViewCellStyle)
  84. {
  85. this.Font = dataGridViewCellStyle.Font;
  86. this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
  87. this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
  88. }
  89. // Implements the IDataGridViewEditingControl.EditingControlRowIndex
  90. // property.
  91. public int EditingControlRowIndex
  92. {
  93. get
  94. {
  95. return rowIndex;
  96. }
  97. set
  98. {
  99. rowIndex = value;
  100. }
  101. }
  102. // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey
  103. // method.
  104. public bool EditingControlWantsInputKey(
  105. Keys key, bool dataGridViewWantsInputKey)
  106. {
  107. // Let the DateTimePicker handle the keys listed.
  108. switch (key & Keys.KeyCode)
  109. {
  110. case Keys.Left:
  111. case Keys.Up:
  112. case Keys.Down:
  113. case Keys.Right:
  114. case Keys.Home:
  115. case Keys.End:
  116. case Keys.PageDown:
  117. case Keys.PageUp:
  118. return true;
  119. default:
  120. return false;
  121. }
  122. }
  123. // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit
  124. // method.
  125. public void PrepareEditingControlForEdit(bool selectAll)
  126. {
  127. // No preparation needs to be done.
  128. }
  129. // Implements the IDataGridViewEditingControl
  130. // .RepositionEditingControlOnValueChange property.
  131. public bool RepositionEditingControlOnValueChange
  132. {
  133. get
  134. {
  135. return false;
  136. }
  137. }
  138. // Implements the IDataGridViewEditingControl
  139. // .EditingControlDataGridView property.
  140. public DataGridView EditingControlDataGridView
  141. {
  142. get
  143. {
  144. return dataGridView;
  145. }
  146. set
  147. {
  148. dataGridView = value;
  149. }
  150. }
  151. // Implements the IDataGridViewEditingControl
  152. // .EditingControlValueChanged property.
  153. public bool EditingControlValueChanged
  154. {
  155. get
  156. {
  157. return valueChanged;
  158. }
  159. set
  160. {
  161. valueChanged = value;
  162. }
  163. }
  164. // Implements the IDataGridViewEditingControl
  165. // .EditingPanelCursor property.
  166. public Cursor EditingPanelCursor
  167. {
  168. get
  169. {
  170. return base.Cursor;
  171. }
  172. }
  173. protected override void OnValueChanged(EventArgs eventargs)
  174. {
  175. // Notify the DataGridView that the contents of the cell
  176. // have changed.
  177. valueChanged = true;
  178. this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
  179. base.OnValueChanged(eventargs);
  180. }
  181. }

*****************************************************************

调用,和DataGridViewTextBoxColumn一样

private CalendarColumn awardsDate;

this.awardsDate = new CalendarColumn();

this.awardsDate.DataPropertyName = "awardsDate";
    this.awardsDate.HeaderText = "颁奖日期";
    this.awardsDate.Name = "awardsDate";

this.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.awardsDate});

可以新增、赋值、编辑该列。