これは間違ったアプローチだと思います。実行時にコントロールの型を気にするべきではありません。データグリッドが構築されたとき、データグリッド列は日付ピッカー型である必要があります。
残念ながら、MS はそれを提供していません。幸いなことに、MS はそれを可能にするフレームワークを提供しています。
新しい 1) DataGridViewColumn
、新しい 2) DataGridViewTextBoxCell
、そして最後に実際の 3) DateTimePicker 編集コントロールの 3 つが必要です。
1:
public class DataGridViewCalendarColumn : DataGridViewColumn
{
public DataGridViewCalendarColumn()
: base(new DataGridViewCalendarCell())
{ }
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(DataGridViewCalendarCell)))
throw new InvalidCastException("Must be a DataGridViewCalendarCell");
base.CellTemplate = value;
}
}
}
2:
public class DataGridViewCalendarCell : DataGridViewTextBoxCell
{
public DataGridViewCalendarCell()
: base()
{
this.Style.Format = "d"; //short date style
}
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);
DataGridViewCalendarControl ctl = DataGridView.EditingControl as DataGridViewCalendarControl;
//use default value if Value is null
if (this.Value == null || this.Value == DBNull.Value)
ctl.Value = (DateTime) this.DefaultNewRowValue;
else
ctl.Value = (DateTime) this.Value;
}
public override Type EditType
{
get
{
//return the type of control this cell uses
return typeof(DataGridViewCalendarControl);
}
}
public override Type ValueType
{
get
{
//return the type of the value that this cell contains
return typeof(DateTime);
}
}
public override object DefaultNewRowValue
{
get
{
//use today's date as the default value
return DateTime.Now;
}
}
}
3:
public class DataGridViewCalendarControl : DateTimePicker, IDataGridViewEditingControl
{
private DataGridView dataGridView;
private bool hasValueChanged = false;
int rowIndex;
public DataGridViewCalendarControl()
{
this.Format = DateTimePickerFormat.Short;
}
protected override void OnValueChanged(EventArgs eventargs)
{
//Notify the DataGridView that the value has changed
hasValueChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnValueChanged(eventargs);
}
#region IDataGridViewEditingControl Members
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
}
public DataGridView EditingControlDataGridView
{
get { return dataGridView; }
set { dataGridView = value; }
}
public object EditingControlFormattedValue
{
get { return this.Value.ToShortDateString(); }
set
{
if (value is String)
try
{
this.Value = DateTime.Parse((String) value);
}
catch
{
this.Value = DateTime.Now;
}
}
}
public int EditingControlRowIndex
{
get { return rowIndex; }
set { rowIndex = value; }
}
public bool EditingControlValueChanged
{
get { return hasValueChanged; }
set { hasValueChanged = value; }
}
public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
{
//the DateTimePicker needs to handle the keys
switch (keyData & Keys.KeyCode)
{
case Keys.Left:
case Keys.Right:
case Keys.Up:
case Keys.Down:
case Keys.Home:
case Keys.End:
case Keys.PageUp:
case Keys.PageDown:
return true;
default:
return !dataGridViewWantsInputKey;
}
}
public Cursor EditingPanelCursor
{
get { return base.Cursor; }
}
public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
public void PrepareEditingControlForEdit(bool selectAll)
{
//nowt needs doing...
}
public bool RepositionEditingControlOnValueChange
{
get { return false; }
}
次に、フォームの Load イベントで:
eventDateDataGridViewCalendarColumn = new DataGridViewCalendarColumn();
eventDateDataGridViewCalendarColumn.DataPropertyName = "EventDate";
eventDateDataGridViewCalendarColumn.HeaderText = "Date";
//we've created the column, now insert it into the right location (after the ID and UserID)
this.tSEventsDataGridView.Columns.Insert(2, eventDateDataGridViewCalendarColumn);
//remove the original TextBox EventDate column added via VS
this.tSEventsDataGridView.Columns.RemoveAt(3);
はどこeventDateDataGridViewCalendarColumn
ですかprivate DataGridViewCalendarColumn
。これで、編集イベントがなくなりました。
これで問題なく動作しますが、ネット上にあるランダムなサンプルと同様に、自己責任で使用してください。