public
class DataGridViewKeyMapBoxCell : DataGridViewComboBoxCell
{
//private static readonly KeyMapBox editingControl = editingControl = new KeyMapBox();
public DataGridViewKeyMapBoxCell()
{
}
/// <summary>
/// Gets the type of the cell's hosted editing control.
/// </summary>
/// <value></value>
/// <returns>The <see cref="T:System.Type"/> of the underlying editing control. As implemented in this class, this property is always null.</returns>
public override Type EditType
{
get
{
// Return the type of the editing contol that CalendarCell uses.
return typeof(DataGridViewKeyMapBoxEditingControl);
}
}
/// <summary>
/// Gets or sets the data type of the values in the cell.
/// </summary>
/// <value></value>
/// <returns>The <see cref="T:System.Type"/> of the cell's value.</returns>
public override Type ValueType
{
get
{
// Return the type of the value that CalendarCell contains.
return typeof(string);
}
set
{
base.ValueType = value;
}
}
/// <summary>
/// Gets the type of the formatted value associated with the cell.
/// </summary>
/// <value></value>
/// <returns>A <see cref="T:System.Type"/> object representing display value type of the cell, which is the <see cref="T:System.Drawing.Image"/> type if the <see cref="P:System.Windows.Forms.DataGridViewImageCell.ValueIsIcon"/> property is set to false or the <see cref="T:System.Drawing.Icon"/> type otherwise.</returns>
public override Type FormattedValueType
{
get
{
return typeof(KeyMapBox);
}
}
//private static void SetKeyMapBoxText(KeyMapBox c, string text)
//{
// c.Text = text;
//}
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
KeyMapBox ctl = DataGridView.EditingControl as KeyMapBox;
if (ctl != null)
{
ctl.Text =
Convert.ToString(initialFormattedValue);
DataGridViewKeyMapBoxColumn owningColumn = OwningColumn as DataGridViewKeyMapBoxColumn;
if (owningColumn != null)
{
ctl.DataSource = owningColumn.CustomDataSource;
ctl.KeyValueColumn.KeyColumn = owningColumn.KeyColumn;
ctl.KeyValueColumn.ValueColumn = owningColumn.ValueColumn;
ctl.OwningCell =
this;
}
//editingControl.Text = value as string;
}
}
/// <summary>
/// Returns a graphic as it would be displayed in the cell.
/// </summary>
/// <param name="value">The value to be formatted.</param>
/// <param name="rowIndex">The index of the cell's parent row.</param>
/// <param name="cellStyle">The <see cref="T:System.Windows.Forms.DataGridViewCellStyle"/> in effect for the cell.</param>
/// <param name="valueTypeConverter">A <see cref="T:System.ComponentModel.TypeConverter"/> associated with the value type that provides custom conversion to the formatted value type, or null if no such custom conversion is needed.</param>
/// <param name="formattedValueTypeConverter">A <see cref="T:System.ComponentModel.TypeConverter"/> associated with the formatted value type that provides custom conversion from the value type, or null if no such custom conversion is needed.</param>
/// <param name="context">A bitwise combination of <see cref="T:System.Windows.Forms.DataGridViewDataErrorContexts"/> values describing the context in which the formatted value is needed.</param>
/// <returns>
/// An object that represents the formatted image.
/// </returns>
protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
{
// need to do this to display value in non edited cell
return value;
}
#region
Handlers of edit events, copied from DataGridViewTextBoxCell
/// <summary>
/// Called when the focus moves to a cell.
/// </summary>
/// <param name="rowIndex">The index of the cell's parent row.</param>
/// <param name="throughMouseClick">true if a user action moved focus to the cell; false if a programmatic operation moved focus to the cell.</param>
protected override void OnEnter(int rowIndex, bool throughMouseClick)
{
base.OnEnter(rowIndex, throughMouseClick);
}
protected override void OnLeave(int rowIndex, bool throughMouseClick)
{
base.OnLeave(rowIndex, throughMouseClick);
}
/// <summary>
/// </summary>
/// <param name="e">A <see cref="T:System.Windows.Forms.DataGridViewCellMouseEventArgs"/> that contains the event data.</param>
protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
{
base.OnMouseClick(e);
base.DataGridView.BeginEdit(false);
}
/// <summary>
/// Determines if edit mode should be started based on the given key.
/// </summary>
/// <param name="e">A <see cref="T:System.Windows.Forms.KeyEventArgs"/> that represents the key that was pressed.</param>
/// <returns>
/// true if edit mode should be started; otherwise, false. The default is false.
/// </returns>
public override bool KeyEntersEditMode(KeyEventArgs e)
{
return base.KeyEntersEditMode(e);
//return (((((char.IsLetterOrDigit((char)((ushort)e.KeyCode)) && ((e.KeyCode < Keys.F1) || (e.KeyCode > Keys.F24))) || ((e.KeyCode >= Keys.NumPad0) && (e.KeyCode <= Keys.Divide))) || (((e.KeyCode >= Keys.OemSemicolon) && (e.KeyCode <= Keys.OemBackslash)) || ((e.KeyCode == Keys.Space) && !e.Shift))) && (!e.Alt && !e.Control)) || base.KeyEntersEditMode(e));
}
#endregion
}