Filter Listbox based on Textbox entry
Filed in C#, Code Snippets on May.27, 2010
Here is the 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; namespace WindowsFormsApplication2 { public partial class Form1 : Form { DataSet ds = new DataSet(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // sample data ds.ReadXml(@"C:\mypath\customers.xml"); // initial customers list // since the textbox is empty, it will show all the customers ShowCustomers(ds.Tables[0], textBox1.Text); } private void ShowCustomers(DataTable odt, string filter) { // need to copy the exact schema for temperory datatable DataTable dt = odt.Clone(); foreach (DataRow dr in odt.Select("Label like '" + filter + "%'")) { dt.ImportRow(dr); } listBox1.DataSource = dt; listBox1.DisplayMember = "Label"; listBox1.ValueMember = "Value"; } private void textBox1_KeyUp(object sender, KeyEventArgs e) { ShowCustomers(ds.Tables[0], textBox1.Text); } } }
Related posts:























Leave a Reply
You must be logged in to post a comment.