Simple AutoComplete ComboBox in a DataGridView

Today I will show you how to create an AutoComplete ComboBox in the DataGridView, add the ComboBox on the DataGridView and add a list of items in the ComboBox programmatically. Let’s begin: Open the Visual Basic 2008, create a project and set your Form just like this. Main Form Then, double click the Form and create a code for adding and putting a list of items in the ComboBox on the DataGridView.
  1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.         'Declaring variable as new DataGridViewComboBoxColumn
  3.          'That represents a column in the datagridview.
  4.         Dim combo As New DataGridViewComboBoxColumn
  5.         'Putting a list of items in the combobox.
  6.         With combo
  7.             .Items.Add("Mark")
  8.             .Items.Add("Marie")
  9.             .Items.Add("Maybel")
  10.             .Items.Add("Mikmik")
  11.             .Items.Add("Minmin")
  12.             .Items.Add("Mely")
  13.             .Items.Add("Mentor")
  14.         End With
  15.         'Adding a combobox in the column on the datagridview.
  16.         DataGridView1.Columns.Add(combo)
  17.  
  18.     End Sub
Go back to the Design Views, double click the DataGridView and select the method name of the DataGridView in EditingControlShowing. After that, do the code for the AutoComplete ComboBox. Method Name
  1. Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
  2.         'declare variable(cb) as a combobox
  3.         Dim cb As ComboBox
  4.         'e represent the editing control in the datagridview
  5.         'the condition is, if the type of e is combobox then set your code for autocomplete
  6.         If TypeOf e.Control Is ComboBox Then
  7.             cb = e.Control
  8.             'set the dropdown style of a combobox
  9.             cb.DropDownStyle = ComboBoxStyle.DropDown
  10.             'set the propety of a combobox to autocomplete mode.
  11.             cb.AutoCompleteMode = AutoCompleteMode.SuggestAppend
  12.             cb.AutoCompleteSource = AutoCompleteSource.ListItems
  13.         End If
  14.     End Sub
You can download the complete Source Code and run it on your computer.

Add new comment