-4

I want to search more columns from the DatagridView. I am able to only search the product name, but I also want to search the Brand name and Category name. How can I achieve this?

Here is my code:

 public void LoadProducts()
    {
        int i = 0;
        dataGridView1.Rows.Clear();
        cn.Open();
        cm = new SqlCommand("select p.pcode, p.barcode, p.pdesc, b.brand, c.category, p.price1, p.price2, p.qty from tblproduct as p inner join tblbrand as b on b.id = p.bid inner join tblcategory as c on c.id = p.cid where p.pdesc like '" + txtSearch.Text + "%' order by p.pdesc", cn);
        dr = cm.ExecuteReader();
        while (dr.Read())
        {
            i++;
            dataGridView1.Rows.Add(i, dr[0].ToString(), dr[1].ToString(), dr[2].ToString(), dr[3].ToString(), dr[4].ToString(), dr[5].ToString(), dr[6].ToString(), dr[7].ToString());
        }
        dr.Close();
        cn.Close();
    }

1 Answers1

-1

After a thorough research, I finally got the solution:

public void LoadProducts()
    {
        int i = 0;
        dataGridView1.Rows.Clear();
        cn.Open();
        cm = new SqlCommand("select p.pcode, p.barcode, p.pdesc, b.brand, c.category, p.price1, p.price2, p.qty from tblproduct as p inner join tblbrand as b on b.id = p.bid inner join tblcategory as c on c.id = p.cid where p.pdesc like '" + txtSearch.Text + "%' or b.brand like '" + txtSearch.Text + "%' or c.category like '" + txtSearch.Text + "%' order by p.pdesc", cn);
        dr = cm.ExecuteReader();
        while(dr.Read())
        {
            i++;
            dataGridView1.Rows.Add(i, dr[0].ToString(), dr[1].ToString(), dr[2].ToString(), dr[3].ToString(), dr[4].ToString(), dr[5].ToString(), dr[6].ToString(), dr[7].ToString());
        }
        dr.Close();
        cn.Close();
    }