По запросу, это мой тестовый код:
MainForm не имеет ничего, кроме DataGridView
, созданного в конструкторе.Функциональность DateTimePicker
создается с использованием универсальной структуры столбцов DataGridViews для создания пользовательских типов ячеек / столбцов.
Когда запускается тестовое приложение, я нажимаю F2
, чтобы начать редактирование, изменить значение с помощью клавиш курсорано затем нажмите ESC
, чтобы отменить редактирование, а затем оно возвращается к исходному значению и удаляет элемент управления редактирования из сетки, оставляя только текстовое поле собственного отображения сетки.
MainForm.cs
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
DataGridView dataGridView1 = new DataGridView();
this.Controls.Add(dataGridView1);
dataGridView1.Columns.Clear();
DataGridViewDateTimeColumn col = new DataGridViewDateTimeColumn();
dataGridView1.Columns.Add(col);
}
}
DataGridViewDateTimeCell.cs
public class DataGridViewDateTimeCell : DataGridViewTextBoxCell
{
Object _defaultNewRowValue;
Boolean _defaultNewRowValueSet;
public DataGridViewDateTimeCell(Object defaultNewRowValue) : base()
{
_defaultNewRowValueSet = true;
_defaultNewRowValue = defaultNewRowValue;
}
public DataGridViewDateTimeCell() : this(DateTime.Now)
{
_defaultNewRowValueSet = false;
}
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
try
{
// Set the value of the editing control to the current cell value.
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
DataGridViewDateTimeEditingControl ctl = DataGridView.EditingControl as DataGridViewDateTimeEditingControl;
if (this.Value != DBNull.Value && this.Value != null)
{
ctl.Value = Convert.ToDateTime(this.Value);
}
}
catch
{
// Do nothing with the exception
}
}
// Return the type of the editing contol that CalendarCell uses.
public override Type EditType { get { return typeof(DataGridViewDateTimeEditingControl); } }
// Return the type of the value that CalendarCell contains.
public override Type ValueType { get { return typeof(DateTime); } }
public override object DefaultNewRowValue
{
get
{
if (_defaultNewRowValueSet)
{
// Use the value given to us as the Default New Row Value
return _defaultNewRowValue;
}
else
{
// Use the current date and time as the default value.
return DateTime.Now;
}
}
}
public override Object Clone()
{
DataGridViewDateTimeCell retVal = base.Clone() as DataGridViewDateTimeCell;
retVal._defaultNewRowValueSet = this._defaultNewRowValueSet;
retVal._defaultNewRowValue = this._defaultNewRowValue;
return retVal;
}
}
DataGridViewDateTimeColumn.cs
public class DataGridViewDateTimeColumn : DataGridViewColumn
{
public DataGridViewDateTimeColumn() : base(new DataGridViewDateTimeCell())
{
base.CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
base.SortMode = DataGridViewColumnSortMode.Automatic;
}
public DataGridViewDateTimeColumn(Object defaultNewRowValue) : base(new DataGridViewDateTimeCell(defaultNewRowValue))
{
// Does nothing
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
// Ensure that the cell used for the template is a DataGridViewDateTimeCell.
if (value != null &&
!value.GetType().IsAssignableFrom(typeof(DataGridViewDateTimeCell)))
{
throw new InvalidCastException("Must be a DataGridViewDateTimeCell");
}
base.CellTemplate = value;
}
}
}
DataGridViewDateTimeEditingControl.cs
public class DataGridViewDateTimeEditingControl : DateTimePicker, IDataGridViewEditingControl
{
public DataGridViewDateTimeEditingControl()
{
this.Format = DateTimePickerFormat.Short;
}
// Implements the IDataGridViewEditingControl.EditingControlFormattedValue property.
public object EditingControlFormattedValue
{
get { return this.Value.ToShortDateString(); }
set
{
if (value is String)
{
this.Value = DateTime.Parse((String)value);
}
}
}
// 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;
if (dataGridViewCellStyle.Format != "d")
{
this.Format = DateTimePickerFormat.Custom;
this.CustomFormat = dataGridViewCellStyle.Format;
}
}
// Implements the IDataGridViewEditingControl.EditingControlRowIndex property.
public int EditingControlRowIndex { get; set; }
// 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:
case Keys.Tab:
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; set; }
// Implements the IDataGridViewEditingControl.EditingControlValueChanged property.
public bool EditingControlValueChanged { get; set; }
// Implements the IDataGridViewEditingControl.EditingPanelCursor property.
public Cursor EditingPanelCursor { get { return base.Cursor; } }
protected override void OnKeyPress(KeyPressEventArgs e)
{
// Notify the DataGridView that the contents of the cell have changed.
EditingControlValueChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnKeyPress(e);
}
protected override void OnCloseUp(EventArgs eventargs)
{
// Notify the DataGridView that the contents of the cell have changed.
EditingControlValueChanged = true;
// setting Value for date if format is Month and Year only
if (!string.IsNullOrEmpty(this.CustomFormat))
{
if (this.CustomFormat.IndexOf("d") < 0)
{
this.Value = new DateTime(this.Value.Year, this.Value.Month, DateTime.DaysInMonth(this.Value.Year, this.Value.Month));
}
}
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnCloseUp(eventargs);
}
protected override void OnValueChanged(EventArgs eventargs)
{
// Notify the DataGridView that the contents of the cell have changed.
EditingControlValueChanged = true;
// setting Value for date if format is Month and Year only
if (!string.IsNullOrEmpty(this.CustomFormat))
{
if (this.CustomFormat.IndexOf("d") < 0)
{
this.Value = new DateTime(this.Value.Year, this.Value.Month, DateTime.DaysInMonth(this.Value.Year, this.Value.Month));
}
}
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnValueChanged(eventargs);
}
}