两个文件:

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});

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

转载于:https://www.cnblogs.com/wanzhongjun/p/6250073.html

DataGridView控件内建立日期选择编辑列相关推荐

  1. [转贴]原创控件代码共享--日期选择控件

    思路:实现日期年月日的选择 1.可以设定年的起止年份 2.排除不正确日期选择的可能 3.使用javascript实现控制 4.使用Text属性方便获取设置日期值 =================== ...

  2. 深入了解DataGridView控件

     DataGridView控件 DataGridView是用于Windows Froms 2.0的新网格控件.它可以取代先前版本中DataGrid控件,它易于使用并高度可定制,支持很多我们的用户需 ...

  3. DataGridView控件使用大全

    原文地址为: DataGridView控件使用大全   DataGridView控件 DataGridView是用于Windows Froms 2.0的新网格控件.它可以取代先前版本中DataGrid ...

  4. WinForm中关于DataGridView控件的一些应用

    转载于新浪 美林居士 的博客:            blog.sina.com.cn/s/blog_797a56d20101daiw.html4 在.NET4.0中,以表格形式存储的数据通常是Dat ...

  5. C# DataGridView控件的基础应用实例

    目录 引言 一.界面简介 二.初始化 三.添加一行数据 四.允许修改表格 五.复制选择的数据 六.复制所有数据 七.读一行数据 八.读所有数据 九.查找名字记录 十.删除一行数据 十一.删除多行数据 ...

  6. winform datagridview控件设置列标题字体大小无效问题

    在datagridview控件的columnHeadersDefaultCellStyle属性中设置列标题字体样式后,非运行时看有效,运行时则无效,主要是因为datagridview控件放在panel ...

  7. 【Android从零单排系列十一】《Android视图控件——日历、日期、时间选择控件》

    目录 一.日历.日期.时间组件基本介绍 二.几种常见的控件类型 1.CalendarView –日历控件 2. DatePicker –日期选择控件 3.TimePicker –时间选择控件 4.Ch ...

  8. JSP页面的日期控件可以弹出选择框选择日期

    JSP页面的日期控件可以弹出选择框选择日期 input框的如下 <input type="text" name="starttime" readonly= ...

  9. C# DataGridView控件用法

    方法一: int index = this.dataGridView1.Rows.Add(); this.dataGridView1.Rows[index].Cells[1].Value = &quo ...

最新文章

  1. R语言使用apriori算法进行关联规则挖掘实战:关联规则概念、频繁项集、支持度(support)、置信度(confidence)、提升度(lift)、apriori算法
  2. php7简短而安全的数组遍历方法
  3. spring aop 注入源码解析 1
  4. boost::hana::all用法的测试程序
  5. C# 动态创建数据库三(MySQL)
  6. ios开发网络学习:一:NSURLConnection发送GET,POST请求
  7. oracle 关于归档的视图,oracle 与归档日志相关的几个视图
  8. Visio 2016专业版 激活方式
  9. Web服务压力测试工具BullBench
  10. ps——霓虹灯字体效果
  11. java输出pdf(pdfptable和pdftcell)
  12. hp暗夜精灵2Pro(HP OMEN 15-ax219TX 暗影精灵 II 代Pro游戏本)驱动列表
  13. 看懂logcat日志
  14. 2012-11-26四六级词汇#9317;-----…
  15. 知到网课艺术与审美考试试题以及答案
  16. 如何在html中制作个人简历表单
  17. Ubuntu16.04安装cello
  18. matlab修改图片位深度_BMP位图32位转为24位深度
  19. 十年时光 离开的谷歌给中国互联网界留下了这些人
  20. 《MongoDB在信息资源共享建设的应用实践》

热门文章

  1. typedef函数指针使用方法
  2. JMeter + influxdb + grafana框架安装
  3. u3d无锯齿遮罩shader-可用于ugui
  4. insta经典滤镜下载
  5. sencha touch tabsidebar 源码扩展
  6. 精益与敏捷开发(随笔)
  7. mfc 资源视图无法打开RC2104
  8. Centos 安装 MySql
  9. SpringCloud 介绍
  10. python3 练习题100例 (二十二)输入两个字符串,输出两个字符串集合的并集