Here I created a function for which the name of the table is passed from which automatic completion should be performed, the name of the field that should be autocompleted, and the combo box that needs to be configured.
Try using the following code:
public void AutoCompleteTextBox(string tableName, string fieldName, ComboBox combToAutoComp)
{
AutoCompleteStringCollection txtCollection = new AutoCompleteStringCollection();
DataTable dtAutoComp = Dal.ExecuteDataSetBySelect("Stored_Procedure", fieldName, tableName);
if (dtAutoComp.Rows.Count >= 0)
{
for (int count = 0; count < dtAutoComp.Rows.Count; count++)
{
txtCollection.Add(dtAutoComp.Rows[count][fieldName].ToString());
}
}
combToAutoComp.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
combToAutoComp.AutoCompleteSource = AutoCompleteSource.CustomSource;
combToAutoComp.AutoCompleteCustomSource = txtCollection;
}
Here Dal.ExecuteDataSetBySelectis my implementation where I create a connection, a command and a dataadapter to call a stored procedure. You can replace it with your own implementation for the same. See this link for more details.
source
share