简体   繁体   English

使用 combobox 按数据网格中的列过滤数据

[英]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我正在尝试按统计信息列进行过滤,其中包含 3 种类型的数据“有效”、“未使用”和“请求购买”,但是当我尝试仅过滤时,我无法按这三种类型进行过滤一个有效,但三个一起不能过滤 none。如果有人能帮我,代码有什么问题

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");
        }

There is an obvious bug in your code.您的代码中存在明显的错误。

There is no {} after your if, you should write your code in a code block. if 后面没有 {},您应该将代码写在代码块中。

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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM