Filter dropdown
I want to have dropdown which bind from the database. It should have filter functionality such as if i write in textbox letter "S" then all item from "S" in drop down it will filter. so please help me out.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit ChoudharyRank #145 · 15.8K reputationPosted Aug 31, 2011, 1:59 AM EDT
Is it a windows forms control, or Asp.net control or WPF etc.
Ajay45 reputationPosted Aug 31, 2011, 2:07 AM EDT
Satyapriya NayakRank #47 · 31.5K reputationPosted Aug 31, 2011, 3:18 AM EDT
Here is a windows form solution for your questions?
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.OleDb;
namespace filter_dropdown
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
OleDbDataAdapter oledbda;
DataSet ds;
string str;
DataTable dt;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from student";
com = new OleDbCommand(str, con);
OleDbDataReader reader = com.ExecuteReader();
while (reader.Read())
{
comboBox1.Items.Add(reader["sname"]);
}
reader.Close();
con.Close();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
comboBox1.Items.Clear();
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select sname from student where sname like '" + textBox1.Text + "%'";
com = new OleDbCommand(str, con);
oledbda = new OleDbDataAdapter(com);
ds = new DataSet();
oledbda.Fill(ds, "student");
dt = ds.Tables["student"];
int i;
for (i = 0; i <= dt.Rows.Count - 1; i++)
{
comboBox1.Items.Add(dt.Rows[i].ItemArray[0]);
}
con.Close();
}
}
}
Thanks
If this post helps you mark it as answer