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

发布于 2024-11-04 06:17:54 字数 1075 浏览 3 评论 0原文

我正在尝试根据 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 之后的值遇到问题,其中它声称是错误的。任何帮助都会很棒。

谢谢

I am trying to filter a binding source based on 2 comboboxes. I have one of comboboxes filter just fine. The second has me stumped as it uses the first combobox and then the second based on a switch statement:

    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;


        }
    }

I am having issues with the value after the AND in which it claims is wrong. Any help would be great.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日久见人心 2024-11-11 06:17:54

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

I think there were some string concatenation issues and you were missing the closing parenthesis around Finisher = -1.

Try

 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;


        }

Note if you have too much string concatenation going on, it's best that you use string.Format(). It improves readability and is more efficient that concatenation. As an example, you first case would look like this

propertyInformationBindingSource.Filter = 
      string.Format("ClientKey ='{0}' And Search = -1", comboBox1.SelectedValue);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文