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.
- Drag and drop a Datagrid control to your C# Windows Application.
- Find Data Grid’s EditingControlShowing event using the Properties window.
- Create a TextBox for auto completion, using DataGridViewEditingControlShowingEventArgs object.
- Check for exact column where we want to place auto completion box.
- Create auto completion string set using AutoCompleteStringCollection class.
- 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?