As we know , can facilitate Autocomplete feature to Text box, DatagridView controls in C#.Net. We can also fetch auto text from data tables too, by using dataview object.
- To do this we need to fill the dataset and dataview objects then add the rows to string collection as follows.
GroupTableAdapter = new SqlDataAdapter("select name from AccountGroups", con); GroupTableDataset = new DataSet(); GroupTableAdapter.Fill(GroupTableDataset, "AccountGroups"); GroupTableView = new DataView(GroupTableDataset.Tables[0]);
- Adding the rows to the string collection
AutoCompleteStringCollection autotext = new AutoCompleteStringCollection(); foreach (DataRowView row in Common.GroupTableView) { autotext.Add(row["name"].ToString()); }
- Attach the collection to the control
txt_gname.AutoCompleteMode = AutoCompleteMode.Suggest; txt_gname.AutoCompleteSource = AutoCompleteSource.CustomSource; txt_gname.AutoCompleteCustomSource = autotext;
drop all three section of code in load or initialize event and it will work.