I have a DGV, connected to a DataSet which is connected to a database, with 8 columns. One of these columns is a combobox with the following settings
At the moment the autocomplete (by default) works like this: If I type in 'a' I get all entries starting with 'a'. But that's it. If I type in 'An' it does not go to e.g. Andalusia.
I already checked the combobox settings, where you can set the autocomplete mode and source and so an but then I saw, that I do not have these settings within the properties you can see above.
Does anyone know how I can get the autocomplete I would like to have accomplished?
You can handle
EditingControlShowing
[1] event of DataGridView
and using Control
property of the event argument, get
DataGridViewComboBoxEditingControl
[2] which is derived from ComboBox
.
Then you can set its
AutoCompleteMode
[3] to available options. You also should set its
DropDownStyle
[4] property to ComboBoxStyle.DropDown
to let the user type in control.
void grid_EditingControlShowing(object s, DataGridViewEditingControlShowingEventArgs e)
{
var comboBox = e.Control as DataGridViewComboBoxEditingControl;
if(comboBox!=null)
{
comboBox.DropDownStyle = ComboBoxStyle.DropDown;
comboBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
}
}
[1] https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview.editingcontrolshowing?WT.mc_id=DT-MVP-5003235&view=netframework-4.8