C# .NET - Filter ListView using TextBox in C# window forms

Asked By Austine on 19-Aug-11 05:57 AM
Hi, Please please i need help on how to filter listview with one TextBox in C# window forms.
 Below is my code .Please spot for me what am doing wrong to correct it.

I used one textBox, one ListView and a Button on the form.

private void Form3_Load(object sender, EventArgs e)
        {
             //scop.Disconnect();
            StringBuilder bui = new StringBuilder();
         
            ScopServXAPI.Response.ActionResponse ActionResult = XAPI.Action_ListExtensions(textBox1.Text);
            string str1 = null;
            listView1.View = View.Details;
            listView1.Columns.Add("",20);
            listView1.Columns.Add("Names", 150);
            listView1.Columns.Add("Extension", 150);
            listView1.Columns.Add("Status", 150);
            try
            {
                if (ActionResult.IsSuccess)
                {
                    foreach (Hashtable ItemData in ActionResult.ListItems)
                    {
                        ScopServXAPI.Data.ExtensionData ExtData = new ScopServXAPI.Data.ExtensionData();
                        ExtData.InitializeFromHashtable(ItemData);
                        ScopServAMI.Response.ExtensionStateResponse ActionResults = scop.Action_ExtensionState(ExtData.Extension, ExtData.Context);
                        //string str1 = null;

                        if (ActionResults.Status.ToString() == "-1")
                        {
                            str1 = ActionResults.Status.ToString("Extension Not  Found");
                        }
                        else
                        {
                            if (ActionResults.Status.ToString() == "0")
                            {
                                str1 = ActionResults.Status.ToString("Idle");
                            }
                            else
                            {
                                if (ActionResults.Status.ToString() == "1")
                                {
                                    str1 = ActionResults.Status.ToString("In Use");
                                }
                                else
                                {
                                    if (ActionResults.Status.ToString() == "2")
                                    {
                                        str1 = ActionResults.Status.ToString("Busy");
                                    }
                                    else
                                    {
                                        if (ActionResults.Status.ToString() == "4")
                                        {
                                            str1 = ActionResults.Status.ToString("Unavailable");
                                        }
                                        else
                                        {
                                            if (ActionResults.Status.ToString() == "8")
                                            {
                                                str1 = ActionResults.Status.ToString("Ringing");
                                            }
                                            else
                                            {
                                                if (ActionResults.Status.ToString() == "16")
                                                {
                                                    str1 = ActionResults.Status.ToString("On Hold");
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        sample = new DataTable();
                        sample.Columns.Add("Names");
                        sample.Columns.Add("Extension");
                        sample.Columns.Add("Status");

                        //sample.Rows.Add("3", "alpha");
                        //sample.Rows.Add("4", "beat");
                        //sample.Rows.Add("5", "ball");
                        //sample.Rows.Add("6", "cat");
                        //sample.Rows.Add("7", "catch");
                        //sample.Rows.Add("10", "zebra");
                        //sample.Rows.Add(ExtData.CallerID, ExtData.Extension, str1);
                      

                        ListViewItem item = new ListViewItem();

                        item.SubItems.Add(ExtData.CallerID);
                        item.SubItems.Add(ExtData.Extension);
                        item.SubItems.Add(str1);
                        //sample.Rows.Add(sample.Rows[1](ExtData.CallerID));
                        //sample.Rows.Add(sample.Rows[2](ExtData.Extension));
                        //sample.Rows.Add(sample.Rows[3](str1));
                        sample.Rows.Add(item.SubItems.ToString());
                        listView1.Items.Add(item);
                        
                        //addItems();

                        textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
                    }
                   
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void textBox1_TextChanged(object sender, EventArgs e)
        {
           
            listView1.Items.Clear(); //Clear all the Data in the ListView
            foreach (DataRow row in sample.Rows)
            {
                if (row["Names"].ToString().StartsWith(textBox1.Text)) //If the cell value is start with the value in the TextBox
                {
                    ListViewItem item = new ListViewItem(row["Names"].ToString());
                    item.SubItems.Add(row["Extension"].ToString());
                    item.SubItems.Add(row["Status"].ToString());
                    listView1.Items.Add(item); //Add this row to the ListView
                }
            }
           
        }

        private void addItems()
        {
            listView1.BeginUpdate();
            listView1.Items.Clear(); //Clear all the Data in the ListView
           
            foreach (DataRow row in sample.Rows)
            {
               
                    ListViewItem item = new ListViewItem(row[0].ToString());
                    item.SubItems.Add(row[1].ToString());
                    item.SubItems.Add(row[2].ToString());
                    listView1.Items.Add(item); //Add this row to the ListView
                
            }
            listView1.EndUpdate();
        }

   Currently is displaying but not filtering with the Datatable.

Please Help. or give me working Examples.
Anoop S replied to Austine on 19-Aug-11 06:38 AM

Filter Listbox based on Textbox entry ->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);
        }
    }
}
dipa ahuja replied to Austine on 19-Aug-11 07:35 AM
Try this :

string conn = "ConnectionString";
    
private void listFilter_Load(object sender, EventArgs e)
{
  SqlDataAdapter da = new SqlDataAdapter("select full_name from tb1", conn);
  DataTable dt = new DataTable();
  da.Fill(dt);
  listBox1.DataSource = dt;
  listBox1.DisplayMember = "full_name";
}
    
private void textBox1_TextChanged(object sender, EventArgs e)
{
  SqlDataAdapter da = new SqlDataAdapter("select full_name from tb1 where full_name like '%" 
    + textBox1.Text + "%'", conn);
  DataTable dt = new DataTable();
  da.Fill(dt);
  listBox1.DataSource = dt;
  listBox1.DisplayMember = "full_name";
}



Search By firstname