'How do I create a textBox autoCompleteSource from dataset table?

In my Winform I have a TextBox. I set it to "AutoComplete", and I want to know what to write in my code (c#), to set it's "AutoCompleteSource" to some column's values in a table from the DataSet. Thanks!



Solution 1:[1]

In the code:

var rows = table.Rows.Cast<DataRow>();
var source = new AutoCompleteStringCollection();
source.AddRange(rows.Select(x => x.Field<String>("ColumnName")).ToArray());
textBox.AutoCompleteCustomSource = source;
textBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

Just replace table, textBox and "ColumnName".

Solution 2:[2]

fill datasource using this post:

Source = dt.AsEnumerable().Select<System.Data.DataRow, String>(x => x.Field<String>("Name")).ToArray();

and this will suggest you

textbox.AutoCompleteCustomSource = source;
textbox.AutoCompleteMode = AutoCompleteMode.Suggest;
textbox.AutoCompleteSource = AutoCompleteSource.CustomSource;

Solution 3:[3]

Assuming you have a data table in table variable, then to show list of it's columns as autocomplete source, you can use a code like this:

var columns = table.Columns.Cast<DataColumn>();
var source = new AutoCompleteStringCollection();
source.AddRange(columns.Select(x => x.ColumnName).ToArray());
textBox1.AutoCompleteCustomSource = source;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

However, using a ComboBox makes more sense. To see an example for ComboBox take a look at this post.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Reza Aghaei