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 ...
随机推荐
- android 绑定spinner键值对显示内存地址的问题
初学android,估计是.net学傻了,觉得android好麻烦. 绑定下拉菜单Spinner键值对. 参照这篇文章,地址:http://blog.csdn.net/shouliang52000/a ...
- 思考力——提升企业竞争力的核心因素
如果你对项目管理.系统架构有兴趣,请加微信订阅号"softjg",加入这个PM.架构师的大家庭 · 思考力就是竞争力:在这个科技飞跃进步的时代,很多事物是我们未曾经历也难以预料的. ...
- oracle 实现ID自增
CREATE TABLE testTable1 ( ID INT NOT NULL, NAME ) NOT NULL, PRIMARY KEY(ID) ) TABLESPACE MYDB; --创建自 ...
- C#笔试(程序设计)
1.如何把一个Array复制到ArrayList里,如何把ArrayList复制到Array里? foreach( object o in array )arrayList.Add(o); Array ...
- javascript运行模式:并发模型 与Event Loop
看了阮一峰老师的JavaScript 运行机制详解:再谈Event Loop和[朴灵评注]的文章,查阅网上相关资料,把自己对javascript运行模式和EVENT loop的理解整理下,不一定对,日 ...
- editplus工具支持sql高亮提示
editplus默认不识别sql关键件,添加文件使其对sql高亮提示. 首先就是要自己编写一段代码,存为.stx 文件(例如sql.stx) 然后在editplus的菜单栏 工具-> 配置用户工 ...
- webform JS打印方法
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DaYin.aspx.cs&qu ...
- MC的内存管理和删除机制
先看一下,什么叫做内存的碎片化: 如果用c语言直接 malloc,free 来向操作系统申请和释放内存时, 在不断的申请和释放过程中,形成了一些很小的内存片断,无法再利用. 这种空闲,但无法利用内存的 ...
- 如何优化cocos2d程序的内存使用和程序大小
在我完成第一个游戏项目的时候,我深切地意识到"使用cocos2d来制作游戏的开发者们,他们大多会被cocos2d的内存问题所困扰".而我刚开始接触cocos2d的时候,社区里面的人 ...
- python基础知识8——模块1——自定义模块和第三方开源模块
模块的认识 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需 ...