mikybrain
mikybrain

Reputation: 169

How to filter datagridview using multiple checkboxes

I've a datagridview which gets its data from a database and three checkBoxes which should be used as a filter.

I want to filter the datagridview by checking one or more of the checkedboxes and should display me the selected item in the datagridview with it relating Amounts:

datagridview

checkBoxes

My code:

           if (cb11.Checked == true)
        {
            try
            {
                //Check an see what's in the dgv
                DataView dv = new DataView(dt);
                dv.RowFilter = " [AreaCode] = " + cb11.Text.Trim();
                datagridview1.DataSource = dv;
            }
            catch (Exception)
            {
                MessageBox.Show("Can’t find the column", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

This give me only the values for 11 How can i join the other one so the i can select multiples ?

Upvotes: 1

Views: 5587

Answers (2)

Shar1er80
Shar1er80

Reputation: 9041

Without seeing code that you have tried, I'll just give an explanation of what you can try.

Assuming that you fetch the data, from the database, only once:

  1. Get your data from the database and store it in a list
  2. Each time you check/uncheck checkboxes

    2.1. Clear your datagridview

    2.2. Iterate through your list and populate your datagridview based on the checked checkboxes.

If you're fetching the data multiple times:

  1. Each time you check/uncheck checkboxes

    1.1. Clear your datagridview

    1.2. Construct a query based on the checked checkboxes

    1.3. Query for your data, and populate your datagridview

EDIT:

So with the code you provided, try creating a string that you'll set to the RowFilter after evaluating all check boxes.

        string rowFilter = string.Empty;
        if (cb11.Checked)
        {
            rowFilter += " [AreaCode] = " + cb11.Text.Trim();
        }
        if (cb16.Checked)
        {
            if (rowFilter.Length > 0)
                rowFilter += " OR";
            rowFilter += " [AreaCode] = " + cb16.Text.Trim();
        }
        if (cb31.Checked)
        {
            if (rowFilter.Length > 0)
                rowFilter += " OR";
            rowFilter += " [AreaCode] = " + cb31.Text.Trim();
        }

        try
        {
            //Check an see what's in the dgv
            DataView dv = new DataView(dt);
            dv.RowFilter = rowFilter;
            datagridview1.DataSource = dv;
        }
        catch (Exception)
        {
            MessageBox.Show("Can’t find the column", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

Upvotes: 1

Zohar Peled
Zohar Peled

Reputation: 82474

Try this:

string RowFilter = string.Empty;
CreateOrAppendToFilter(cb11, ref RowFilter);
CreateOrAppendToFilter(cb16, ref RowFilter);
CreateOrAppendToFilter(cb31, ref RowFilter);
  if(RowFilter.Length > 0) 
  {
     try
        {
            //Check an see what's in the dgv
            DataView dv = new DataView(dt);
            dv.RowFilter = RowFilter;
            datagridview1.DataSource = dv;
        }
        catch (Exception)
        {
            MessageBox.Show("Can’t find the column", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
  }

    private void CreateOrAppendToFilter(CheckBox cb, ref string RowFilter) 
    { 
        if(RowFilter.Length>0) 
        {
            RowFilter += " OR ";
        }
        RowFilter += (cb.Checked) ? string.Format("[AreaCode] = {0}", cb.Text.Trim()) : string.Empty ;
    }

Upvotes: 0

Related Questions