Add the following two classes to the same namespace of form1:
# Region C # dview ComboBox editable
// The class to be added
Public class datagridviewcomboeditboxcolumn: datagridviewcomboboxcolumn
{
Public datagridviewcomboeditboxcolumn ()
{
Datagridviewcomboeditboxcell OBJ = new datagridviewcomboeditboxcell ();
This. celltemplate = OBJ;
}
}
// The class to be added
Public class datagridviewcomboeditboxcell: datagridviewcomboboxcell
{
Public override void initializeeditingcontrol (INT rowindex, object initialformattedvalue, datagridviewcellstyle)
{
Base. initializeeditingcontrol (rowindex, initialformattedvalue, datagridviewcellstyle );
ComboBox = (ComboBox) base. datagridview. editingcontrol;
If (ComboBox! = NULL)
{
ComboBox. dropdownstyle = comboboxstyle. dropdown;
ComboBox. autocompletemode = autocompletemode. Suggest;
ComboBox. Validating + = new canceleventhandler (combobox_validating );
}
}
Protected override object getformattedvalue (object value, int rowindex, ref datagridviewcellstyle cellstyle, typeconverter valuetypeconverter, typeconverter formattedvaluetypeconverter, datagridviewdataerrorcontexts context)
{
If (value! = NULL)
{
If (value. tostring (). Trim ()! = String. Empty)
{
If (items. indexof (value) =-1)
{
Items. Add (value );
Datagridviewcomboboxcolumn Col = (datagridviewcomboboxcolumn) owningcolumn;
Col. Items. Add (value );
}
}
}
Return base. getformattedvalue (value, rowindex, ref cellstyle, valuetypeconverter, formattedvaluetypeconverter, context );
}
Void combobox_validating (Object sender, system. componentmodel. canceleventargs E)
{
Datagridviewcomboboxeditingcontrol CBO = (datagridviewcomboboxeditingcontrol) sender;
If (CBO. Text. Trim () = string. Empty) return;
Datagridview grid = CBO. editingcontroldatagri;
Object value = CBO. text;
// Add Value to list if not there
If (CBO. Items. indexof (value) =-1)
{
Datagridviewcomboboxcolumn cbocol = (datagridviewcomboboxcolumn) grid. Columns [grid. currentcell. columnindex];
// Must add to both the current ComboBox as well as the template, to avoid duplicate entries
CBO. Items. Add (value );
Cbocol. Items. Add (value );
Grid. currentcell. value = value;
}
}
}
# Endregion