1

I have a gridView control named dgvAllBikes. On form load i load dgvAllBikes. Now i want to filter this gridView which is dgvAllBikes on basis of three options and a search button.

1) CC

2) Model

3) Color

When i click on search button i want to filter dgvAllBikes on basis of these three options.

Here is my LoadGrid Code

 private DataTable LoadGrid()
    {

        using (SqlConnection con = new SqlConnection(CS))
        {
            SqlCommand cmd = new SqlCommand("spGetAllBikes", con);
            cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
            SqlDataReader r = cmd.ExecuteReader();
            dtAllBike = new DataTable();

            dtAllBike.Load(r);

        }
        return dtAllBike;
    }

i have declared DataTable in class level

string CS;
    protected DataTable dtAllBike;
    public SaleBike()
    {
        InitializeComponent();
        CS = ConfigurationManager.ConnectionStrings["BikeDB"].ConnectionString;
    }

Here is the picture enter image description here

Here is the code for btnSearch.

private void btnSearch_Click(object sender, EventArgs e)
    {
        BindingSource bs = new BindingSource();
        bs.DataSource = dgvAllBikeDetails.DataSource;
        bs.Filter = dgvAllBikeDetails.Columns["CC"].HeaderText.ToString() + " LIKE '%" + tbCCSearch.Text + "%'";
        dgvAllBikeDetails.DataSource = bs;

It filter on basis of CC but i am not able to bind the other two option. Your help will be really appreciated.

1 Answer 1

5

Use the OR or AND operator to create a filter that works on multiple values.

private void btnSearch_Click(object sender, EventArgs e)
{
    BindingSource bs = new BindingSource();
    bs.DataSource = dgvAllBikeDetails.DataSource;   

    string filter = "";

    // Check if text fields are not null before adding to filter. 
    if (!string.IsNullOrEmpty(tbCCSearch.Text)) 
    {
        filter += dgvAllBikeDetails.Columns["CC"].HeaderText.ToString() + " LIKE '%" + tbCCSearch.Text + "%' ";
    }
    if (!string.IsNullOrEmpty(tbModelSearch.Text)) 
    {
        if (filter.length > 0) filter += "OR ";
        filter += dgvAllBikeDetails.Columns["Model"].HeaderText.ToString() + " LIKE '%" + tbModelSearch.Text + "%' ";
    }
    if (!string.IsNullOrEmpty(tbCCSearch.Text)) 
    {
        if (filter.length > 0) filter += "OR ";
        filter += dgvAllBikeDetails.Columns["Color"].HeaderText.ToString() + " LIKE '%" + tbColorSearch.Text + "%' ";
    }

    bs.Filter = filter;
    dgvAllBikeDetails.DataSource = bs;
}
2
  • Superb.. Love it .. Thank you very much bro
    – Af'faq
    Apr 17, 2017 at 14:25
  • 1
    Glad I could help! =) Apr 17, 2017 at 14:25

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.