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});
可以新增、赋值、编辑该列。
DataGridView控件内建立日期选择编辑列的更多相关文章
- 038. asp.netWeb用户控件之六实现日期选择的用户控件
web用户控件的ascx代码: <%@ Control Language="C#" AutoEventWireup="true" CodeFile=&qu ...
- DataGridView控件
DataGridView控件 DataGridView是用于Windows Froms 2.0的新网格控件.它可以取代先前版本中DataGrid控件,它易于使用并高度可定制,支持很多我们的用户需要的特 ...
- DataGridView控件-[引用]
DataGridView控件 DataGridView是用于Windows Froms 2.0的新网格控件.它可以取代先前版本中DataGrid控件,它易于使用并高度可定制,支持很多我们的用户需要的特 ...
- DataGridView控件使用大全说明-各种常用操作与高级操作
DataGridView控件 DataGridView是用于Windows Froms 2.0的新网格控件.它可以取代先前版本中DataGrid控件,它易于使用并高度可定制,支持很多我们的用户需要的特 ...
- DataGridView控件使用大全
转自:http://www.cnblogs.com/xiaofengfeng/archive/2011/04/16/2018504.html DataGridView控件 DataGridView是用 ...
- ADO.NET之使用DataGridView控件显示从服务器上获取的数据
今天回顾下ADO.NET中关于使用DataGridiew控件显示数据的相关知识 理论整理: 使用 DataGridView 控件,可以显示和编辑来自多种不同类型的数据源的表格数据. SqlDataAd ...
- 实现DataGridView控件中CheckBox列的使用
最近做WindowsForms程序,使用DataGridView控件时,加了一列做选择用,发现CheckBox不能选中.搜索后,要实现DataGridView的CellContentClick事件,将 ...
- DataGridView控件用法一:数据绑定
使用DataGridView控件,可以显示和编辑来自多种不同类型的数据源的表格数据. 将数据绑定到DataGridView控件非常简单和直观,在大多数情况下,只需设置DataSource属性即可.在绑 ...
- 在DataGridView控件中实现冻结列分界线
我们在使用Office Excel的时候,有很多时候需要冻结行或者列.这时,Excel会在冻结的行列和非冻结的区域之间绘制上一条明显的黑线.如下图: (图1) WinForm下的DataGridVie ...
随机推荐
- ubuntu 13.10 svn工具 rabbitvcs 安装
ubuntu 版本:13.10:桌面模式默认:unity :文件管理器:nautilus
- AX7: HOW TO USE CLASS EXTENSION METHODS
AX7: HOW TO USE CLASS EXTENSION METHODS To create new methods on a standard AX class without custo ...
- ReferenceError: $ is not defined (绝对路径和相对路径)
今天使用JQuery时,进入js文件后一直达不到想要的效果,在firebug上调试后发现报错提示"ReferenceError: $ is not defined",显然,这是JQ ...
- VS 设置编译后的程序可以以管理员身份运行
1.首先,创建一个文件命名为 XXX.exe.manifest, 并将以下内容复制到文件 <?xml version="1.0" encoding="UTF-8&q ...
- Opencv配置问题_Error LNK2019
终于配好opencv(Win7 64位+VS2013+opencv2.4.9),兴奋的写了第一个程序(当然是显示lena的玉照了): #include <opencv2\opencv.hpp&g ...
- OC Runtime
OC 是面向运行时的语言.Runtime就是系统在运行的时候的一些机制,其中最主要的是消息发送机制.OC语言与其他语言(如C语言)在函数(方法)的调用有很大的不同.C语言,函数的调用在编译的时候就已经 ...
- IAR调节字体大小
在主面板上点击tools->Options,然后点开Editor,选择下面的Colors and Fonts选项,最后选右上方的Font,选择要设置的字体就OK了.
- SIGABRT的可能原因
常见原因: 第三方库如glic检测到内部错误或者破坏约束条件 3种可能1.double free/free 没有初始化的地址或者错误的地址2.堆越界3.assert
- Python:利用内建函数将字符串转化为整数
使用内建函数raw_input()内建函数,它读取标准输入,并将读取到的数据赋值给指定的变量.我们可以使用int()内建函数将用户输入的字符串转换为整数: >>> user = ra ...
- XtraBackup原理解读
XtraBackup是现今为止唯一一款为InnoDB 和XtraDB提供热备的开源工具,这个工具有以下的有点: (1)备份快速高效而且可靠 (2)备份过程可以做到事物处理不间断 (3)节省磁盘空间和网 ...