Search code examples
c#asp.netgridviewdataviewrowfilter

How to merge multiple Row Filter Records in a single Gridview using Checkbox


I want to retrieve specific records from an excel file uploaded in a gridview on the basis of specific conditions mentioned in checkboxes. My code works perfectly when one checkbox is checked but it doesn't respond/search records in case of multiple checkbox selection and only show records in gridview based on just one checkbox. As far as my understanding it has something to do with "Rowfilter" property.

Following is the code that i have in my Search button.

private void Btn_Search_Click(object sender, EventArgs e)
 {
             dt = Form1.dataRecord;
             DataView dv = new DataView(dt);
             foreach (DataRow dr in dt.Rows)
             {
                 if (Chk_CoS != null & Chk_CoS.Checked == true)
                 {
                     dv.RowFilter = "[COUNTRY OF SHIPMENT] = 'XYZ'";
                     LoadSNumber();
                     dataGridView2.DataSource = dv;
                 }
                 if (Chk_Amount != null & Chk_Amount.Checked == true)
                 {
                     dv.RowFilter = "[BILL AMOUNT] < [AMOUNT FINANCED]";
                     LoadSNumber();
                     dataGridView2.DataSource = dv;
                 }
                 if (Chk_Date != null & Chk_Date.Checked == true)
                 {
                     dv.RowFilter = "[Date of Finance] < [InvoiceDate]";
                     LoadSNumber();
                     dataGridView2.DataSource = dv;
                 }
               
             }
        }

        private void Chk_Date_CheckedChanged(object sender, EventArgs e)
        {
           
        }

        private void Chk_Amount_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void Chk_CoS_CheckedChanged(object sender, EventArgs e)
        {
    
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

Solution

  • So, we might have 1, or 4 check boxes. If they are checked, THEN we want that filter.

    If un-checked, we don't care.

    So what you do is build up a string based filter for each check box AND THEN apply/create/use the filter.

    That way, the following code can work for 2 or 16 check boxes - it don't matter.

    AND WHY is there a for/each for setting the filter? You don't need some for each for the filter - you ahve some filter options, you set them, and then setup a filter. I do NO reson for a for each data row here? (very confusing????).

    So, you have a datatable, you setup a filter, then apply that filter. I don't see any need for a fro each row. That filter against the table applies to ALL rows anyway.

    So the code idea, code approach, code concept will look like this:

    Note VERY carefull how this code would work for 2 or 15 check boxes. We simply build up, add over each selected option to build ONE filter that has ALL of the options + filter you checked and want to include.

    This thus should give you the idea, the keys to heaven here as to how this can work:

        dt = MyTable;     - whatever and how your table is - set here.
        string strFilter = "";
        DataView dv = new DataView(dt);
    
        if (Chk_CoS != null & Chk_CoS.Checked == true)
            strFilter = "([COUNTRY OF SHIPMENT] = 'XYZ' ) ";
    
        if (Chk_Amount != null & Chk_Amount.Checked == true)
        {
            if (strFitler != "")
                strFilter += " AND ";
            strFilter += "([BILL AMOUNT] < [AMOUNT FINANCED]) ";
        }
    
        if (Chk_Date != null & Chk_Date.Checked == true)
        {
            if (strFitler != "")
                strFilter += " AND ";
    
            strFilter += "([Date of Finance] < [InvoiceDate])";
        }
    
        // Now apply filter
    
        dv.RowFilter = strFilter;
    
        DataGridView2.DataSource = dv;
        DataGridView2.DataBind();
    }
    

    So note how this could/would work for 2 or 15 filter choices. We simply build up the filter for each option into a string. If the filter check box is not selected, then nothing occurs - we just keep building and ONLY build for each check box option.

    Once we have the filter string, then ONE operation is required to filter against the data table, and we thus apply the data-view to the grid control.