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 ...
随机推荐
- php使用mysql和mysqli连接查询数据
mysql: <?php $code = $_POST['code']; $status = ""; $success = ""; $scookies = ...
- PHP数据类型
在PHP中,一共支持8种数据类型:整型,浮点型,布尔型,字符串型,数组,对象,空类型(NULL),资源型 标量类型 int(integet)整数类型 整型数据:在内存中占4个字节,也就是32个bit位 ...
- js模版解析
function JzRender(tpl, data) { // 模版解析 data是对象则返回字符串,是数组则返回字符串数组 if (data instanceof Array) { var s ...
- leetcode-【简单题】Happy Number
题目: Write an algorithm to determine if a number is "happy". A happy number is a number def ...
- 图片链接hover移动的解决方案分析
现实在网站中,我们经常看到有鼠标滑过某个图片,出现边框的效果.如下图:
- PSP个人(观众界面)
需求:作为一个观众,我希望了解某一场比赛的比分,以便了解赛况.(满意条件:精确到每一局的结果比分) 需求分析:实现查询数据库中每一局的分数并用界面显示. 生成设计文档: 运用三层架构,实现软件的基本功 ...
- (分享)多功能 PDF转换器v3.0版本
转换的效果非常不错,值得使用.破解成功的截图:这个程序必须随便输入注册码注册,不然会有水印的. 这是程序主界面了 正在测试pdf转word过程,转换结果个人感觉非常不错,跟原版pdf的格式非常接近,个 ...
- js工厂方式和构造函数
工厂方式 //工厂方式 : 封装函数 function createPerson(name){ //1.原料 var obj = new Object(); //2.加工 obj.name = nam ...
- wc命令
wc命令的功能为统计指定文件中的字节数.字数.行数, 并将统计结果显示输出. 语法:wc [选项] 文件- 说明:该命令统计给定文件中的字节数.字数.行数.如果没有给出文件名,则从标准输入读取.wc同 ...
- Orcle基本语句(三)
COMMIT; --查询表内所有内容 SELECT * FROM stu_info; --查询部分列,并赋予别名 SELECT stu_id 学生标号,stu_name 学生姓名 FROM stu_i ...