Auto complete Box

Add auto complete box for datagrid in C#


As we learned how to add an auto complete option for TextBox control in C#, we can also add this feature where a Text entry works. We are back with Datagrid example here. All you have to do is follow these steps.

  1. Drag and drop a Datagrid control to your C# Windows Application.
  2. Find Data Grid’s  EditingControlShowing event using the Properties window.
  3. Create a TextBox for auto completion, using DataGridViewEditingControlShowingEventArgs object.
  4. Check for exact column where we want to place auto completion box.
  5. Create auto completion string set using AutoCompleteStringCollection class.
  6. Fill Auto completion Mode,Source and custom source properties

The Code

 String t = dataGridView1.Columns[1].HeaderText;
            if (t.Equals(“Items”))
            {
                TextBox auto = e.Control as TextBox;
                if (auto!=null) {
                    AutoCompleteStringCollection autotxt = new AutoCompleteStringCollection();
                    auto.AutoCompleteMode = AutoCompleteMode.Suggest;
                    auto.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    autotxt.Add(“Citrizine”);
                    autotxt.Add(“CDT”);
                    autotxt.Add(“DAXIRGIN”);
                    autotxt.Add(“CX4”);
                    autotxt.Add(“DNT-Tab”);
                    auto.AutoCompleteCustomSource = autotxt;
                }
            }
It’s looks like simple, sin’t it?

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.