How to bind (populate) Bind (Populate) AutoComplete ComboBox from Database in Windows Forms application using C#.
The population of an AutoComplete ComboBox is done in same way as the normal ComboBox, just the AutoCompleteMode needs to be enabled.
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Windows.Forms;
using
System.Configuration;
using
System.Data.SqlClient;
namespace
Face_rekognitaion
{
public
partial
class
AutoComplateComboBox
: Form
{
public
AutoComplateComboBox()
{
InitializeComponent();
}
private
void
AutoComplateComboBox_Load(object
sender, EventArgs
e)
{
string
constr = @"Data
Source=.;Initial Catalog=app_abc;Integrated Security=True";
using
(SqlConnection
con = new
SqlConnection(constr))
{
using
(SqlDataAdapter
sda = new
SqlDataAdapter("SELECT
empid, name FROM tab_emp",
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]
= 0;
row[1]
= "";
dt.Rows.InsertAt(row,
0);
//Assign
DataTable as DataSource.
cmboxname.DataSource
= dt;
cmboxname.DisplayMember
= "name";
cmboxname.ValueMember
= "empid";
//Set
AutoCompleteMode.
cmboxname.AutoCompleteMode
= AutoCompleteMode.Suggest;
cmboxname.AutoCompleteSource
= AutoCompleteSource.ListItems;
}
}
}
private
void
btnSubmit_Click(object
sender, EventArgs
e)
{
string
message = "Employee
Name: "
+ cmboxname.Text;
message
+= Environment.NewLine;
message
+= "EmployeeID:
"
+ cmboxname.SelectedValue;
MessageBox.Show(message);
}
}
}
No comments:
Post a Comment