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

本文关键字:条件 问题 存在 bindingsource filter | 更新日期: 2023-09-27 17:54:38

我正在尝试基于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之后的值有问题,其中它声称是错误的。如果有任何帮助就太好了。

谢谢

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

我认为有一些字符串连接问题,你错过了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()。它提高了可读性,并且比串联更有效。作为一个例子,您的第一个case看起来像这样

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