Autocomplete text to a single datagridview column in C#

We already discussed the matter how to suggest AutoComplte Text  for datagridview column in C#. This post is the extension to that post, which will explain , limit the searching capability for single column.

Task: Need Autocomplete suggest list of Text as I type for the PARTY column only[DataGridView].


Drop the following code in the  dataGridView1_EditingControlShowing event.

TextBox auto = e.Control as TextBox;
 if (dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].HeaderText == "PARTY")
 AutoCompleteStringCollection autotxt = new AutoCompleteStringCollection();

foreach (DataRowView row in Common.AccountRegistrationTableView)
 auto.AutoCompleteMode = AutoCompleteMode.Suggest;
 auto.AutoCompleteSource = AutoCompleteSource.CustomSource;
 auto.AutoCompleteCustomSource = autotxt;
 auto.AutoCompleteMode = AutoCompleteMode.None;
 auto.AutoCompleteSource = AutoCompleteSource.None;
 auto.AutoCompleteCustomSource = null;

  • Firstly  capture the control using TextBox auto = e.Control as TextBox;
  • Secondly  populate names from the database and add to AutocompletStringCollection.
  • Thirdly  attach the collection to custom source of the control.
  • The else part simply prevent from displaying autocomplete for every columns in the datagridview.



Published by


A developer,teacher and a blogger obsessed with Python,Dart and open source world from India

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.