DataGridView控件内建立日期选择编辑列
两个文件:
CalendarColumn 类:
- public class CalendarColumn : DataGridViewColumn
- {
- public CalendarColumn()
- : base(new CalendarCell())
- {
- }
- public override DataGridViewCell CellTemplate
- {
- get
- {
- return base.CellTemplate;
- }
- set
- {
- // Ensure that the cell used for the template is a CalendarCell.
- if (value != null &&
- !value.GetType().IsAssignableFrom(typeof(CalendarCell)))
- {
- throw new InvalidCastException("Must be a CalendarCell");
- }
- base.CellTemplate = value;
- }
- }
- }
**********************************************************************
CalendarCell 类:
- public class CalendarCell : DataGridViewTextBoxCell
- {
- public CalendarCell()
- : base()
- {
- // Use the short date format.
- this.Style.Format = "d";
- }
- public override void InitializeEditingControl(int rowIndex, object
- initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
- {
- // Set the value of the editing control to the current cell value.
- base.InitializeEditingControl(rowIndex, initialFormattedValue,
- dataGridViewCellStyle);
- CalendarEditingControl ctl =
- DataGridView.EditingControl as CalendarEditingControl;
- if (this.Value == null)
- ctl.Value = DateTime.Now;
- else
- ctl.Value = (DateTime)this.Value;
- }
- public override Type EditType
- {
- get
- {
- // Return the type of the editing contol that CalendarCell uses.
- return typeof(CalendarEditingControl);
- }
- }
- public override Type ValueType
- {
- get
- {
- // Return the type of the value that CalendarCell contains.
- return typeof(DateTime);
- }
- }
- public override object DefaultNewRowValue
- {
- get
- {
- // Use the current date and time as the default value.
- return DateTime.Now;
- }
- }
- }
- class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
- {
- DataGridView dataGridView;
- private bool valueChanged = false;
- int rowIndex;
- public CalendarEditingControl()
- {
- this.Format = DateTimePickerFormat.Short;
- }
- // Implements the IDataGridViewEditingControl.EditingControlFormattedValue
- // property.
- public object EditingControlFormattedValue
- {
- get
- {
- return this.Value.ToShortDateString();
- }
- set
- {
- String newValue = value as String;
- if (newValue != null)
- {
- this.Value = DateTime.Parse(newValue);
- }
- }
- }
- // Implements the
- // IDataGridViewEditingControl.GetEditingControlFormattedValue method.
- public object GetEditingControlFormattedValue(
- DataGridViewDataErrorContexts context)
- {
- return EditingControlFormattedValue;
- }
- // Implements the
- // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
- public void ApplyCellStyleToEditingControl(
- DataGridViewCellStyle dataGridViewCellStyle)
- {
- this.Font = dataGridViewCellStyle.Font;
- this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
- this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
- }
- // Implements the IDataGridViewEditingControl.EditingControlRowIndex
- // property.
- public int EditingControlRowIndex
- {
- get
- {
- return rowIndex;
- }
- set
- {
- rowIndex = value;
- }
- }
- // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey
- // method.
- public bool EditingControlWantsInputKey(
- Keys key, bool dataGridViewWantsInputKey)
- {
- // Let the DateTimePicker handle the keys listed.
- switch (key & Keys.KeyCode)
- {
- case Keys.Left:
- case Keys.Up:
- case Keys.Down:
- case Keys.Right:
- case Keys.Home:
- case Keys.End:
- case Keys.PageDown:
- case Keys.PageUp:
- return true;
- default:
- return false;
- }
- }
- // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit
- // method.
- public void PrepareEditingControlForEdit(bool selectAll)
- {
- // No preparation needs to be done.
- }
- // Implements the IDataGridViewEditingControl
- // .RepositionEditingControlOnValueChange property.
- public bool RepositionEditingControlOnValueChange
- {
- get
- {
- return false;
- }
- }
- // Implements the IDataGridViewEditingControl
- // .EditingControlDataGridView property.
- public DataGridView EditingControlDataGridView
- {
- get
- {
- return dataGridView;
- }
- set
- {
- dataGridView = value;
- }
- }
- // Implements the IDataGridViewEditingControl
- // .EditingControlValueChanged property.
- public bool EditingControlValueChanged
- {
- get
- {
- return valueChanged;
- }
- set
- {
- valueChanged = value;
- }
- }
- // Implements the IDataGridViewEditingControl
- // .EditingPanelCursor property.
- public Cursor EditingPanelCursor
- {
- get
- {
- return base.Cursor;
- }
- }
- protected override void OnValueChanged(EventArgs eventargs)
- {
- // Notify the DataGridView that the contents of the cell
- // have changed.
- valueChanged = true;
- this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
- base.OnValueChanged(eventargs);
- }
- }
*****************************************************************
调用,和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.可以设定年的起止年份 2.排除不正确日期选择的可能 3.使用javascript实现控制 4.使用Text属性方便获取设置日期值 =================== ...
- 深入了解DataGridView控件
DataGridView控件 DataGridView是用于Windows Froms 2.0的新网格控件.它可以取代先前版本中DataGrid控件,它易于使用并高度可定制,支持很多我们的用户需 ...
- DataGridView控件使用大全
原文地址为: DataGridView控件使用大全 DataGridView控件 DataGridView是用于Windows Froms 2.0的新网格控件.它可以取代先前版本中DataGrid ...
- WinForm中关于DataGridView控件的一些应用
转载于新浪 美林居士 的博客: blog.sina.com.cn/s/blog_797a56d20101daiw.html4 在.NET4.0中,以表格形式存储的数据通常是Dat ...
- C# DataGridView控件的基础应用实例
目录 引言 一.界面简介 二.初始化 三.添加一行数据 四.允许修改表格 五.复制选择的数据 六.复制所有数据 七.读一行数据 八.读所有数据 九.查找名字记录 十.删除一行数据 十一.删除多行数据 ...
- winform datagridview控件设置列标题字体大小无效问题
在datagridview控件的columnHeadersDefaultCellStyle属性中设置列标题字体样式后,非运行时看有效,运行时则无效,主要是因为datagridview控件放在panel ...
- 【Android从零单排系列十一】《Android视图控件——日历、日期、时间选择控件》
目录 一.日历.日期.时间组件基本介绍 二.几种常见的控件类型 1.CalendarView –日历控件 2. DatePicker –日期选择控件 3.TimePicker –时间选择控件 4.Ch ...
- JSP页面的日期控件可以弹出选择框选择日期
JSP页面的日期控件可以弹出选择框选择日期 input框的如下 <input type="text" name="starttime" readonly= ...
- C# DataGridView控件用法
方法一: int index = this.dataGridView1.Rows.Add(); this.dataGridView1.Rows[index].Cells[1].Value = &quo ...
最新文章
- R语言使用apriori算法进行关联规则挖掘实战:关联规则概念、频繁项集、支持度(support)、置信度(confidence)、提升度(lift)、apriori算法
- php7简短而安全的数组遍历方法
- spring aop 注入源码解析 1
- boost::hana::all用法的测试程序
- C# 动态创建数据库三(MySQL)
- ios开发网络学习:一:NSURLConnection发送GET,POST请求
- oracle 关于归档的视图,oracle 与归档日志相关的几个视图
- Visio 2016专业版 激活方式
- Web服务压力测试工具BullBench
- ps——霓虹灯字体效果
- java输出pdf(pdfptable和pdftcell)
- hp暗夜精灵2Pro(HP OMEN 15-ax219TX 暗影精灵 II 代Pro游戏本)驱动列表
- 看懂logcat日志
- 2012-11-26四六级词汇#9317;-----…
- 知到网课艺术与审美考试试题以及答案
- 如何在html中制作个人简历表单
- Ubuntu16.04安装cello
- matlab修改图片位深度_BMP位图32位转为24位深度
- 十年时光 离开的谷歌给中国互联网界留下了这些人
- 《MongoDB在信息资源共享建设的应用实践》