Good day guys .., just wondering if someone can help me out =) ..,
i have a program and i want the listview to display products as i enter text in the search textbox... my data is in the database .. for example when i type 'co' the products {coca cola, coconut oil, and colgate} will be displayed in the list view ...

here's my code ...
METHOD ..

public List<string> getProductName()
        {
            try
            {
                List<string> searchedProds = new List<string>();
                DataRow[] returnedRows;
                returnedRows = productTable.Select("*");
                for (int i = 0; i < returnedRows.Length; i++)
                {
                    DataRow data = returnedRows[i];
                    int id = Int32.Parse(data.ItemArray.GetValue(0).ToString());
                    string categID = data.ItemArray.GetValue(1).ToString();
                    string brandId = data.ItemArray.GetValue(2).ToString();
                    string name = data.ItemArray.GetValue(3).ToString();
                    string desc = data.ItemArray.GetValue(4).ToString();
                    int qty = Int32.Parse(data.ItemArray.GetValue(5).ToString());
                    decimal price = decimal.Parse(data.ItemArray.GetValue(6).ToString());
                    Products prod = new Products(id, categId, brandId, name, desc, qty, price);
                    searchedProds.Add(name);
                }
                return searchedProds;
            }
            catch (Exception)
            {
                return null;
            }

        }

here's my client implementation :

private void textBox_Search_TextChanged(object sender, EventArgs e) {

       
            List<Products> searchedProducts;
            List<string> prods = server.getProductName();

            for (int i = 0; i < prods.Count; i++ )
            {
                searchedProducts = server.getProducts(textBox_Search.Text);
                listView_products.Clear();
                listView_products.Columns.Add("Product ID", -2, HorizontalAlignment.Left);
                listView_products.Columns.Add("Brand ID", -2, HorizontalAlignment.Left);
                listView_products.Columns.Add("Category ID", -2, HorizontalAlignment.Left);
                listView_products.Columns.Add("Product Name", -4, HorizontalAlignment.Left);
                listView_products.Columns.Add("Product Description", -4, HorizontalAlignment.Left);
                listView_products.Columns.Add("Product Count", -2, HorizontalAlignment.Left);
                listView_products.Columns.Add("Product Price", -2, HorizontalAlignment.Left);

                listView_products.AutoResizeColumn(0, ColumnHeaderAutoResizeStyle.HeaderSize);
                listView_products.AutoResizeColumn(1, ColumnHeaderAutoResizeStyle.HeaderSize);
                listView_products.AutoResizeColumn(2, ColumnHeaderAutoResizeStyle.HeaderSize);
                listView_products.AutoResizeColumn(3, ColumnHeaderAutoResizeStyle.HeaderSize);
                listView_products.AutoResizeColumn(4, ColumnHeaderAutoResizeStyle.HeaderSize);
                listView_products.AutoResizeColumn(5, ColumnHeaderAutoResizeStyle.HeaderSize);
                listView_products.AutoResizeColumn(6, ColumnHeaderAutoResizeStyle.HeaderSize);
                listView_products.Items.Add(productItem);
                listView_products.Update();

                if (prods[i].StartsWith(textBox_Search.Text))
                {
                    prods = server.getProductName();
                    for (int j = 0; j < getProductName.Count; j++) {
                        productItem = new ListViewItem(searchedProducts[i].ProductID.ToString());
                        productItem.SubItems.Add(searchedProducts[i].ProductName);
                        productItem.SubItems.Add(searchedProducts[i].ProductDesc);
                        productItem.SubItems.Add(searchedProducts[i].ProductCount.ToString());
                        productItem.SubItems.Add(searchedProducts[i].ProductPrice.ToString());

                        listView_products.Items.AddRange(new ListViewItem[] { productItem });
                    }

                }
                else
                {
                    listView_products.Clear();
                    listView_products.Columns.Add("No Results Found With Your Search.", -2, HorizontalAlignment.Left);
                }


            }

First of all you must decide how you want your input to be matched with data in your database, such as matching upper/lower etc. Afterward handling the proper event should let you do it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.