在bindingsource.filter中出现多个条件的问题

时间:2011-05-01 19:14:23

标签: c# filter bindingsource

我正在尝试根据2个组合框过滤绑定源。我有一个组合框过滤器就好了。第二个让我难过,因为它使用第一个组合框,然后第二个基于switch语句:

    private void comboBox2_SelectedIndexChanged(object sender,
    System.EventArgs e)
    {
        string sItem;
        sItem = comboBox2.SelectedItem.ToString();

        switch (sItem)
        {
            case "Banks":
                propertyInformationBindingSource.Filter = ("ClientKey ='" + comboBox1.SelectedValue + "'" And "Search = -1");
                break;
            case "Exam":
                propertyInformationBindingSource.Filter = ("ClientKey ='" + comboBox1.SelectedValue + "'") And ("Exam = -1");
                break;
            case "Search Finished":
                propertyInformationBindingSource.Filter = ("ClientKey ='" + comboBox1.SelectedValue + "'") And ("Finished = -1;
                break;
            case "All":
                propertyInformationBindingSource.Filter = "ClientKey ='" + comboBox1.SelectedValue + "'";
                break;


        }
    }

我在它声称错误的AND之后的值有问题。任何帮助都会很棒。

由于

1 个答案:

答案 0 :(得分:2)

我认为存在一些字符串连接问题,并且您错过了Finisher = -1周围的右括号。

尝试

 switch (sItem)
        {
            case "Banks":
                propertyInformationBindingSource.Filter = ("ClientKey ='" + comboBox1.SelectedValue + "' And Search = -1");
                break;
            case "Exam":
                propertyInformationBindingSource.Filter = ("ClientKey ='" + comboBox1.SelectedValue + "' And Exam = -1");
                break;
            case "Search Finished":
                propertyInformationBindingSource.Filter = ("ClientKey ='" + comboBox1.SelectedValue + "' And Finished = -1");
                break;
            case "All":
                propertyInformationBindingSource.Filter = "ClientKey ='" + comboBox1.SelectedValue + "'";
                break;


        }

请注意,如果您进行了太多的字符串连接,最好使用string.Format()。它提高了可读性,并且更有效地进行连接。举个例子,你的第一个案例看起来像这样

propertyInformationBindingSource.Filter = 
      string.Format("ClientKey ='{0}' And Search = -1", comboBox1.SelectedValue);