【问题标题】:Issue with multiple criteria in bindingsource.filterbindingsource.filter 中存在多个条件的问题
【发布时间】:2023-03-13 15:44:01
【问题描述】:

我正在尝试根据 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 之后的值有疑问。任何帮助都会很棒。

谢谢

【问题讨论】:

    标签: c# filter bindingsource


    【解决方案1】:

    我认为存在一些字符串连接问题,并且您缺少 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);
    

    【讨论】:

    • @Bala R 语法似乎是正确的,但我仍然没有得到想要的结果。使用 combobox2 时,它总是返回没有记录。
    • @Korrowan 如果你在 switch 块的末尾放置一个断点,propertyInformationBindingSource.Filter 看起来像什么?
    • @Bala R for Case "Banks" 它是 "ClientKey ='3' And Search = -1"
    • ClientKey 的数据类型是什么?如果它不是一个字符串,也许你可以跳过单引号。否则,Filter 表达式对我来说看起来不错。
    • @Bala R 我把单引号去掉了。它似乎只适用于银行......其他一切都被忽略了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 2013-05-22
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 2014-06-23
    相关资源
    最近更新 更多