两个文件:

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

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

DataGridView控件内建立日期选择编辑列的更多相关文章

  1. 038. asp.netWeb用户控件之六实现日期选择的用户控件

    web用户控件的ascx代码: <%@ Control Language="C#" AutoEventWireup="true" CodeFile=&qu ...

  2. DataGridView控件

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

  3. DataGridView控件-[引用]

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

  4. DataGridView控件使用大全说明-各种常用操作与高级操作

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

  5. DataGridView控件使用大全

    转自:http://www.cnblogs.com/xiaofengfeng/archive/2011/04/16/2018504.html DataGridView控件 DataGridView是用 ...

  6. ADO.NET之使用DataGridView控件显示从服务器上获取的数据

    今天回顾下ADO.NET中关于使用DataGridiew控件显示数据的相关知识 理论整理: 使用 DataGridView 控件,可以显示和编辑来自多种不同类型的数据源的表格数据. SqlDataAd ...

  7. 实现DataGridView控件中CheckBox列的使用

    最近做WindowsForms程序,使用DataGridView控件时,加了一列做选择用,发现CheckBox不能选中.搜索后,要实现DataGridView的CellContentClick事件,将 ...

  8. DataGridView控件用法一:数据绑定

    使用DataGridView控件,可以显示和编辑来自多种不同类型的数据源的表格数据. 将数据绑定到DataGridView控件非常简单和直观,在大多数情况下,只需设置DataSource属性即可.在绑 ...

  9. 在DataGridView控件中实现冻结列分界线

    我们在使用Office Excel的时候,有很多时候需要冻结行或者列.这时,Excel会在冻结的行列和非冻结的区域之间绘制上一条明显的黑线.如下图: (图1) WinForm下的DataGridVie ...

随机推荐

  1. 在线编辑器的使用总结(kindeditor , )

    1).kindedtor中让编辑框默认为“HTML代码/源代码”模式 [javascript] view plaincopyprint? <script> // 自定义插件 #1 Kind ...

  2. 【HEVC】2、HM-16.7编码一个CU(帧内部分) 1.帧内预测相邻参考像素获取

    HEVC帧内预测的35中预测模式是在PU基础上定义的,实际帧内预测的过程则以TU为单位.PU以四叉树划分TU,一个PU内所有TU共享同一种预测模式.帧内预测分3个步骤: (1) 判断当前TU相邻像素点 ...

  3. smb:unrecognized service

    [root@sycflash ~]#service smb status smb:unrecognized service 提示:smb:unrecognized service.这时你会按照常规的方 ...

  4. Bootstrap框架(基础篇)

    本文引用慕课网http://www.imooc.com/learn/141  作者大漠  讲的很详细,我没有全篇都引用,其中很多是添加了自己的一些理解. Bootstrap框架是基于JQuery 所以 ...

  5. 腾讯云Centos下Nginx反向代理Apache+Tomcat

    1. 安装Apahce, PHP, MySQL以及php连接mysql库的组件#yum -y install httpd php mysql mysql-server php-mysql     // ...

  6. 洛谷P1710 地铁涨价

    P1710 地铁涨价 51通过 339提交 题目提供者洛谷OnlineJudge 标签O2优化云端评测2 难度提高+/省选- 提交  讨论  题解 最新讨论 求教:为什么只有40分 数组大小一定要开够 ...

  7. 仿QQ消息气泡提醒

    https://github.com/dkmeteor/Bubble-Notification   感谢这位作者,本例子只是封装了一下源码. 这是jar下载地址 下载之后只要引用就好.下面是一个最简单 ...

  8. [转]:C#的ToString如何格式化字符串

    C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString("D5") 00025 E 科学型 25000.ToString ...

  9. Javascript数组函数库

    其实平时用的比较多的应该是push和pop,不过还是都记下来,以便后面使用. shift :删除原数组第一项,并返回删除元素的值:如果数组为空则返回undefined var a = [1,2,3,4 ...

  10. ios10 no route to host

    IdTCPClient1 ios10 no route to host IOS 9 正常 IOS10 出错,参考下面的设置解决问题 http://www.sh-huhe.com/solution/14 ...