I solved it, and this is the full solution:
[ToolboxItem(false)]
public class MCCBEditingControl : MultiColumnsComboBox, IDataGridViewEditingControl
{
private DataGridView _DataGridView;
public MCCBEditingControl()
{
}
public DataGridView EditingControlDataGridView
{
get { return _DataGridView; }
set { _DataGridView = value; }
}
private int rowIndex;
public int EditingControlRowIndex
{
get { return rowIndex; }
set { rowIndex = value; }
}
protected override void OnSelectedValueChanged(EventArgs e)
{
try
{
EditingControlValueChanged = true;
this.EditingControlDataGridView.
NotifyCurrentCellDirty(true);
base.OnSelectedValueChanged(e);
}
catch (Exception)
{
throw;
}
}
public object EditingControlFormattedValue
{
get
{
if (this.SelectedIndex > 0)
{
return this.Text.ToString();
}
else if (this.SelectedIndex == 0)
{
return string.Empty;
}
else
{
return string.Empty;
}
}
set
{
string newValue = value as string;
if (newValue != null)
this.Text = newValue;
}
}
private bool valueChanged = true;
public bool EditingControlValueChanged
{ get => valueChanged; set => valueChanged = value; }
public Cursor EditingPanelCursor =>
base.Cursor;
public bool RepositionEditingControlOnValueChange => false;
public void ApplyCellStyleToEditingControl(
DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.ForeColor = dataGridViewCellStyle.ForeColor;
this.BackColor = dataGridViewCellStyle.BackColor;
}
public bool EditingControlWantsInputKey(
Keys keyData, bool dataGridViewWantsInputKey)
{
switch (keyData & 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.Enter:
return true;
default:
return false;
}
}
public object GetEditingControlFormattedValue(
DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
public void PrepareEditingControlForEdit(bool selectAll)
{
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Tab || keyData == Keys.Enter)
{
this.DroppedDown = false;
if (this.SelectedIndex == 0)
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
public class MCCoboBoxCell : DataGridViewComboBoxCell
{
public override Type EditType => typeof(MCCBEditingControl);
public override Type ValueType => base.ValueType;
public MCCoboBoxCell() : base()
{
}
public override void InitializeEditingControl(
int rowIndex, object initialFormattedValue,
DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(
rowIndex, initialFormattedValue, dataGridViewCellStyle);
MCCBEditingControl ctl=
DataGridView.EditingControl as MCCBEditingControl;
ctl.ShowAddNew = true;
ctl.NewButtonText = NewItemButtonCaption;
ctl.DropDownStyle = ComboBoxStyle.DropDown;
}
public override object DefaultNewRowValue =>
base.DefaultNewRowValue;
private string _NewButtonText ="Add New";
public string NewItemButtonCaption
{
get
{
return _NewButtonText;
}
set
{
_NewButtonText = value;
}
}
public static List<MCCoboBoxCell> mCCoboBoxCells = new List<MCCoboBoxCell>();
public override object Clone()
{
var clone = (MCCoboBoxCell)base.Clone();
clone.NewItemButtonCaption = this.NewItemButtonCaption;
mCCoboBoxCells.Add(clone);
return clone;
}
}
public class MultiColumnComboBox : DataGridViewComboBoxColumn
{
public MultiColumnComboBox()
{
this.CellTemplate = new MCCoboBoxCell();
}
private string _NewButtonText = "Add New Account";
[NotifyParentProperty(true)]
public string NewItemButtonCaption
{
set
{
_NewButtonText = value;
Clone();
foreach (MCCoboBoxCell cell in MCCoboBoxCell.mCCoboBoxCells)
{
cell.NewItemButtonCaption = this.NewItemButtonCaption;
}
}
get
{
return _NewButtonText;
}
}
public static List<MCCoboBoxCell> mCCoboBoxCells = new List<MCCoboBoxCell>();
public override object Clone()
{
var clone = (MCCoboBoxCell)base.CellTemplate;
clone.NewItemButtonCaption = _NewButtonText;
mCCoboBoxCells.Add(clone);
return base.Clone();
}
}