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 ...
随机推荐
- [转]javascript eval函数解析json数据时为什加上圆括号eval("("+data+")")
javascript eval函数解析json数据时为什么 加上圆括号?为什么要 eval这里要添加 “("("+data+")");//”呢? 原因在于: ...
- python IDLE编程时遇到Python Error: Inconsistent indentation detected! 解决方法
仔细检查了几遍代码,发现indent没有错误! 之后试将所有indent都用空格代替,程序就跑起来了. 具体原因可能是IDLE环境内的Tab键有小bug.
- POS管理系统之出入库单分页查询
JSP: <html> <head> <title>My JSP 'inOutKuPage.jsp' starting page</title> ...
- js table的笔记,实现添加 td,实现搜索功能
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- 20条Linux命令面试问答
程序师 http://www.techug.com/20-linux-command-interview-questions 问:1 如何查看当前的Linux服务器的运行级别? 答: ‘who -r ...
- HTML5学习总结
一.HTML5概念 HTML5并不仅仅只是做为HTML标记语言的一个最新版本,更重要的是它制定了Web应用开发的一系列标准,成为第一个将Web做为应用开发平台的HTML语言. HTML5定义了一系列新 ...
- openstack vm_lifecycle
nova instance状态:power_state, vm_state, task_state 2015-09-22 Openstack 185 nova instance有3种状态:power_ ...
- 8、SQL Server 表分区
什么是表分区?表分区其实就是将一个大表分成若干个小表.表分区可以从物理上将一个大表分成几个小表,但是逻辑上还是一个表.所以当执行插入.更新等操作的时候,不需要我们去判断应该插入或更新到哪个表中.只需要 ...
- xml---sax操作
<?xml version="1.0" encoding="UTF-8"?> <书架> <书> <书名>书名1& ...
- CheckBox设置Enabled为False后,无法修改ForeColor
设置CheckBox的AutoCheck为False,Enabled为True,即可解决.