In this article I will explain with an example, how to bind (populate) Bind (Populate) AutoComplete ComboBox from Database in Windows Forms application using C# and VB.Net.
The population of an AutoComplete ComboBox is done in same way as the normal ComboBox, just the AutoCompleteMode needs to be enabled.
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
Form Design
The Form consists of a ComboBox control and a Button. The Button has been assigned Click event handler.
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Bind (Populate) AutoComplete ComboBox from Database using DataTable (DataSet)
Inside the Form Load event, the records from the database are fetched into a DataTable.
Once the DataTable is populated from database, a new row is inserted in First position which will act as the Default (Blank) item for the ComboBox.
Finally the AutoCompleteMode and the AutoCompleteSource properties are set which converts the normal ComboBox to an AutoComplete ComboBox.
C#
private void Form1_Load(object sender, EventArgs e)
{
string constr = @"Data Source=.\SQL2005;Initial Catalog=Northwind;User Id=sa;Password=pass@123";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter("SELECT CustomerId, ContactName FROM Customers", con))
{
//Fill the DataTable with records from Table.
DataTable dt = new DataTable();
sda.Fill(dt);
//Insert the Default Item to DataTable.
DataRow row = dt.NewRow();
row[0] = "";
row[1] = "";
dt.Rows.InsertAt(row, 0);
//Assign DataTable as DataSource.
cbCustomers.DataSource = dt;
cbCustomers.DisplayMember = "ContactName";
cbCustomers.ValueMember = "CustomerId";
//Set AutoCompleteMode.
cbCustomers.AutoCompleteMode = AutoCompleteMode.Suggest;
cbCustomers.AutoCompleteSource = AutoCompleteSource.ListItems;
}
}
}
VB.Net
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Dim constr As String = "Data Source=.\SQL2005;Initial Catalog=Northwind;User Id=sa;Password=pass@123"
Using con As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter("SELECT CustomerId, ContactName FROM Customers", con)
'Fill the DataTable with records from Table.
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
'Insert the Default Item to DataTable.
Dim row As DataRow = dt.NewRow()
row(0) = ""
row(1) = ""
dt.Rows.InsertAt(row, 0)
'Assign DataTable as DataSource.
cbCustomers.DataSource = dt
cbCustomers.DisplayMember = "ContactName"
cbCustomers.ValueMember = "CustomerId"
'Set AutoCompleteMode.
cbCustomers.AutoCompleteMode = AutoCompleteMode.Suggest
cbCustomers.AutoCompleteSource = AutoCompleteSource.ListItems
End Using
End Using
End Sub
Fetching the selected Text and Value of AutoComplete ComboBox
When the Button is clicked, the Selected Text and Value of the AutoComplete ComboBox is fetched and displayed using MessageBox.
C#
private void btnSubmit_Click(object sender, EventArgs e)
{
string message = "Name: " + cbCustomers.Text;
message += Environment.NewLine;
message += "CustomerId: " + cbCustomers.SelectedValue;
MessageBox.Show(message);
}
VB.Net
Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
Dim message As String = "Name: " & cbCustomers.Text
message += Environment.NewLine
message += "CustomerId: " & cbCustomers.SelectedValue
MessageBox.Show(message)
End Sub
Screenshot
Downloads