Click here to Skip to main content
Click here to Skip to main content

AutoComplete ComboBox Control From Database

By , 26 Aug 2010
 
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)

About the Author

alrosan
Software Developer
Jordan Jordan
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionreason for no votememberLloyd Share5 Jul '12 - 3:47 
GeneralReason for my vote of 5 Thank you very much, very clear, sim...membereshaq9 Jul '11 - 11:17 
GeneralReason for my vote of 2 no text, no tip.mvpLuc Pattyn28 Feb '11 - 6:55 
GeneralReason for my vote of 5 goodmemberHeaven20203 Nov '10 - 10:54 
GeneralReason for my vote of 5 its so simple to understamemberdas raj27 Aug '10 - 17:57 

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 26 Aug 2010
Article Copyright 2010 by alrosan
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid