Click here to Skip to main content
15,868,088 members
Articles / Desktop Programming / Windows Forms
Tip/Trick

AutoComplete ComboBox Control From Database

Rate me:
Please Sign up or sign in to vote.
3.60/5 (9 votes)
26 Aug 2010CPOL 52.2K   4   6
C#


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Autocomplete_controls_csharp
{
    public partial class Form1 : Form
    {
        private string connStr = @"data source=.\sqlexpress;database=northwind;integrated security=true";
        DataTable dt;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Configure_ComboBox();
        }
        void Configure_ComboBox()
        {
            this.Connect();
            if (dt == null)
            {
                MessageBox.Show("Error in Quering");
                return;
            }
            IList<string> lstFirst = new List<string>();
            IList<string> lstLast = new List<string>();
            foreach (DataRow row in dt.Rows)
            {                
                lstFirst.Add(row.Field<string>("firstname"));
                lstLast.Add(row.Field<string>("lastname"));
            }                                        
            this.comboBoxFirstName.Items.AddRange(lstFirst.ToArray<string>());
            this.comboBoxFirstName.AutoCompleteMode = AutoCompleteMode.Suggest;
            this.comboBoxFirstName.AutoCompleteSource = AutoCompleteSource.ListItems;

            this.comboBoxLastName.Items.AddRange(lstLast.ToArray<string>());
            this.comboBoxLastName.AutoCompleteMode = AutoCompleteMode.Suggest;
            this.comboBoxLastName.AutoCompleteSource = AutoCompleteSource.ListItems;
        
        }
        void Connect()
        {
            SqlConnection conn = new SqlConnection(this.connStr);
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(@"select firstname,lastname from employees", conn);
                SqlDataAdapter ada = new SqlDataAdapter(cmd);
                dt = new DataTable();
                ada.Fill(dt);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error:" + ex.Message.ToString());
            }
            finally { conn.Close(); }
        }
    }
}



Visual Basic

Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
    Private connStr As String = "data source=.\sqlexpress;database=northwind;integrated security=true"
    Dim dt As DataTable
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Configure_ComboBox()
    End Sub
    Sub Configure_ComboBox()
        Me.Connect()
        If dt Is Nothing Then
            MessageBox.Show("Error in Quering")
            Exit Sub
        End If
        Dim lstFirst As IList(Of String) = New List(Of String)
        Dim lstLast As IList(Of String) = New List(Of String)
        For Each _row As DataRow In dt.Rows
            lstFirst.Add(_row("firstname"))
            lstLast.Add(_row("lastname"))
        Next
        Me.ComboBoxFirstName.Items.AddRange(lstFirst.ToArray)
        Me.ComboBoxFirstName.AutoCompleteMode = AutoCompleteMode.Suggest
        Me.ComboBoxFirstName.AutoCompleteSource = AutoCompleteSource.ListItems
        Me.ComboBoxLastName.Items.AddRange(lstLast.ToArray)
        Me.ComboBoxLastName.AutoCompleteMode = AutoCompleteMode.Suggest
        Me.ComboBoxLastName.AutoCompleteSource = AutoCompleteSource.ListItems
    End Sub
    Sub Connect()
        Dim conn As New SqlConnection(connStr)
        Try
            conn.Open()
            Dim cmd As New SqlCommand("select firstname,lastname from employees", conn)
            Dim ada As New SqlDataAdapter(cmd)
            dt = New DataTable
            ada.Fill(dt)
        Catch ex As Exception
            MessageBox.Show("Error:" & ex.ToString)
        Finally
            conn.Close()
        End Try
    End Sub
End Class

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Jordan Jordan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
PraiseMy Vote of 5 Pin
t.alkahtiri9-Aug-17 7:13
t.alkahtiri9-Aug-17 7:13 
Questionreason for no vote Pin
Lloyd Share5-Jul-12 3:47
Lloyd Share5-Jul-12 3:47 
GeneralReason for my vote of 5 Thank you very much, very clear, sim... Pin
eshaq9-Jul-11 11:17
eshaq9-Jul-11 11:17 
GeneralReason for my vote of 2 no text, no tip. Pin
Luc Pattyn28-Feb-11 6:55
sitebuilderLuc Pattyn28-Feb-11 6:55 
GeneralReason for my vote of 5 good Pin
Global Analyser3-Nov-10 10:54
Global Analyser3-Nov-10 10:54 
GeneralReason for my vote of 5 its so simple to understa Pin
Rajesh kumar das0227-Aug-10 17:57
Rajesh kumar das0227-Aug-10 17:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.