DataGridView扩展:DateTimePicker
2009-09-22 08:57 Zhang_Chen 阅读(545) 评论(1) 编辑 收藏 举报
代码转自CSDN部分修改:
Code
using System;
using System.Windows.Forms;
namespace CuteDataView
{
public class CalendarColumn : DataGridViewColumn
{
public CalendarColumn(): base(new CalendarCell())
{
}
public override DataGridViewCell CellTemplate
{
get { return base.CellTemplate; }
set
{
if (value != null &&!value.GetType().IsAssignableFrom(typeof (CalendarCell)))
{
throw new InvalidCastException("Must be a CalendarCell ");
}
base.CellTemplate = value;
}
}
}
public class CalendarCell : DataGridViewTextBoxCell
{
/// <summary>
/// 编辑控件的类型
/// </summary>
public override Type EditType
{
get { return typeof (CalendarEditingControl); }
}
public override Type ValueType
{
get { return typeof (DateTime); }
}
public override object DefaultNewRowValue
{
get { return DateTime.Now; }
}
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue,
DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
var ctl =DataGridView.EditingControl as CalendarEditingControl;
if (ctl != null) ctl.Text = Value.ToString();
}
//如果自定义Cell有public属性。建议实现Clone方法
public override object Clone()
{
return new CalendarCell();
}
}
internal class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
{
private DataGridView _dataGridView;
private int _rowIndex;
private bool _valueChanged ;
#region IDataGridViewEditingControl Members
public object EditingControlFormattedValue
{
get { return Value.ToLongDateString(); }
set
{
var newValue = value as String;
if (newValue != null)
{
Value = DateTime.Parse(newValue);
}
}
}
public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
Font = dataGridViewCellStyle.Font;
CalendarForeColor = dataGridViewCellStyle.ForeColor;
CalendarMonthBackground = dataGridViewCellStyle.BackColor;
}
public int EditingControlRowIndex
{
get { return _rowIndex; }
set { _rowIndex = value; }
}
public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
{
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;
}
}
public void PrepareEditingControlForEdit(bool selectAll)
{
}
public bool RepositionEditingControlOnValueChange
{
get { return false; }
}
public DataGridView EditingControlDataGridView
{
get { return _dataGridView; }
set { _dataGridView = value; }
}
public bool EditingControlValueChanged
{
get { return _valueChanged; }
set { _valueChanged = value; }
}
public Cursor EditingPanelCursor
{
get { return base.Cursor; }
}
#endregion
protected override void OnValueChanged(EventArgs eventargs)
{
_valueChanged = true;
EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnValueChanged(eventargs);
}
}
}
using System;
using System.Windows.Forms;
namespace CuteDataView
{
public class CalendarColumn : DataGridViewColumn
{
public CalendarColumn(): base(new CalendarCell())
{
}
public override DataGridViewCell CellTemplate
{
get { return base.CellTemplate; }
set
{
if (value != null &&!value.GetType().IsAssignableFrom(typeof (CalendarCell)))
{
throw new InvalidCastException("Must be a CalendarCell ");
}
base.CellTemplate = value;
}
}
}
public class CalendarCell : DataGridViewTextBoxCell
{
/// <summary>
/// 编辑控件的类型
/// </summary>
public override Type EditType
{
get { return typeof (CalendarEditingControl); }
}
public override Type ValueType
{
get { return typeof (DateTime); }
}
public override object DefaultNewRowValue
{
get { return DateTime.Now; }
}
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue,
DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
var ctl =DataGridView.EditingControl as CalendarEditingControl;
if (ctl != null) ctl.Text = Value.ToString();
}
//如果自定义Cell有public属性。建议实现Clone方法
public override object Clone()
{
return new CalendarCell();
}
}
internal class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
{
private DataGridView _dataGridView;
private int _rowIndex;
private bool _valueChanged ;
#region IDataGridViewEditingControl Members
public object EditingControlFormattedValue
{
get { return Value.ToLongDateString(); }
set
{
var newValue = value as String;
if (newValue != null)
{
Value = DateTime.Parse(newValue);
}
}
}
public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
Font = dataGridViewCellStyle.Font;
CalendarForeColor = dataGridViewCellStyle.ForeColor;
CalendarMonthBackground = dataGridViewCellStyle.BackColor;
}
public int EditingControlRowIndex
{
get { return _rowIndex; }
set { _rowIndex = value; }
}
public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
{
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;
}
}
public void PrepareEditingControlForEdit(bool selectAll)
{
}
public bool RepositionEditingControlOnValueChange
{
get { return false; }
}
public DataGridView EditingControlDataGridView
{
get { return _dataGridView; }
set { _dataGridView = value; }
}
public bool EditingControlValueChanged
{
get { return _valueChanged; }
set { _valueChanged = value; }
}
public Cursor EditingPanelCursor
{
get { return base.Cursor; }
}
#endregion
protected override void OnValueChanged(EventArgs eventargs)
{
_valueChanged = true;
EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnValueChanged(eventargs);
}
}
}