Auto-Complete in textbox using data from a SQL Server database:
I am trying to get my textbox to auto-complete when a user types in it to write a query. This would be similar to how SQL Server Management Studio does it and gives the option as you type to arrow down or click on a table name or column name. Here is the following code I have.......
C#(Code).....using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Auto_complet_textbox_shown_data
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void autoCompleteData()
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=c:\users\surendrak\documents\visual studio 2010\Projects\Auto complet textbox shown data\Auto complet textbox shown data\Database1.mdf;Integrated Security=True;User Instance=True");
SqlCommand com = new SqlCommand("Select State from tbl_State", con);
con.Open();
SqlDataReader rdr = com.ExecuteReader();
//AutoCompleteStringCollection Contains a collection of strings to use for the auto-complete feature on certain Windows Forms controls.
AutoCompleteStringCollection autoCompleteCollection = new AutoCompleteStringCollection();
while (rdr.Read())
{
autoCompleteCollection.Add(rdr.GetString(0));
}
//Set AutoCompleteSource property of txt_StateName as CustomSource
txt_StateName.AutoCompleteSource = AutoCompleteSource.CustomSource;
//Set AutoCompleteMode property of txt_StateName as SuggestAppend. SuggestAppend Applies both Suggest and Append
txt_StateName.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
txt_StateName.AutoCompleteCustomSource = autoCompleteCollection;
con.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
autoCompleteData();
}
}
}
Comments
Post a Comment