Search code examples
c#visual-studiofilterdatagridviewcombobox

filter data by a column in datagrid using combobox


I'm trying to filter by the stats column, which contains 3 types of data "valid", "out of use" and "request purchase", but I'm not able to filter by the three, when I try to filter only by one it works, but the three together don't filters none.if anyone can help me what is the problem with the code

code:

private void combo_SelectedIndexChanged(object sender, EventArgs e)
        {           
            if (combo.SelectedIndex == 1) ;
            DataView pp = (dtgridreagentes.DataSource as DataTable).DefaultView;
            pp.RowFilter = string.Format("Stats = '{0}'", "Válido");

            if (combo.SelectedIndex == 2) ;
            DataView rr = (dtgridreagentes.DataSource as DataTable).DefaultView;
            rr.RowFilter = string.Format("Stats = '{0}'", "Fora de uso");

            if (combo.SelectedIndex == 3) ;
            DataView tt = (dtgridreagentes.DataSource as DataTable).DefaultView;
            tt.RowFilter = string.Format("Stats = '{0}'", "Solicitar compra");
        }

Solution

  • There is an obvious bug in your code.

    There is no {} after your if, you should write your code in a code block.

    If you run your code, only the last 2 lines of code will work.

    Correctly modify this question:

    if (combo.SelectedIndex == 1)
    {
        DataView pp = (dtgridreagentes.DataSource as DataTable).DefaultView;
        pp.RowFilter = string.Format("Stats = '{0}'", "Válido");
    }