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].

datagrid

Drop the following code in the  dataGridView1_EditingControlShowing event.

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

foreach (DataRowView row in Common.AccountRegistrationTableView)
 {
 autotxt.Add(row["name"].ToString());
 }
 auto.AutoCompleteMode = AutoCompleteMode.Suggest;
 auto.AutoCompleteSource = AutoCompleteSource.CustomSource;
 auto.AutoCompleteCustomSource = autotxt;
 }
 else
 {
 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.

 

 

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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.