【问题标题】:Better Way To Build BindingSource Filter From a Group of Controls [WinForms]从一组控件构建 BindingSource 过滤器的更好方法 [WinForms]
【发布时间】:2009-12-29 13:54:42
【问题描述】:

现在我正在通过一堆 if 语句构建基于用户输入(文本框和组合框)的过滤器。一定会有更好的办法。这是我当前的代码:

    Private Function BuildProductsFilter() As String

        Dim RawFilterResults As String = ""

        If Not CompanyNameComboBox.SelectedValue Is Nothing AndAlso Not CompanyNameComboBox.SelectedValue.ToString = "[ALL]" Then
            RawFilterResults = "companyname = '" & CompanyNameComboBox.SelectedValue.ToString & "'"
        End If

        If QtyTextbox.Text > "" AndAlso IsNumeric(QtyTextbox.Text) Then
            RawFilterResults &= " and stock = " & QtyTextbox.Text
        End If

        If KeywordTextbox.Text > "" Then
            RawFilterResults &= " and (description like '%" & KeywordTextbox.Text & "%'"
            RawFilterResults &= "or descriptionlong like '%" & KeywordTextbox.Text & "%'"
            RawFilterResults &= "or details like '%" & KeywordTextbox.Text & "%')"
        End If

        If SKUTextbox.Text > "" Then

            If SKUTextbox.Text.StartsWith("*") Then
                RawFilterResults &= " and sku like '%" & SKUTextbox.Text & "'"
            ElseIf SKUTextbox.Text.EndsWith("*") Then
                RawFilterResults &= " and sku like '" & SKUTextbox.Text & "%'"
            Else
                RawFilterResults &= " and sku = '" & SKUTextbox.Text & "'"
            End If

        End If

        If Not AllowPurchaseCombobox.SelectedItem Is Nothing AndAlso Not AllowPurchaseCombobox.SelectedItem.ToString = "[ALL]" Then
            RawFilterResults &= " and allowpurchase = '" & AllowPurchaseCombobox.SelectedValue.ToString & "'"
        End If

        If Not ShowPriceCombobox.SelectedItem Is Nothing AndAlso Not ShowPriceCombobox.SelectedItem.ToString = "[ALL]" Then
            RawFilterResults &= " and showprice = '" & ShowPriceCombobox.SelectedValue.ToString & "'"
        End If

        If Not VirtualLoupeCombobox.SelectedItem Is Nothing AndAlso Not VirtualLoupeCombobox.SelectedItem.ToString = "[ALL]" Then
            RawFilterResults &= " and VirtualLoupe = '" & VirtualLoupeCombobox.SelectedValue.ToString & "'"
        End If

        If ImageTextbox.Text > "" Then
            Dim ImageDir As String = Path.GetDirectoryName(ImageTextbox.Text)
            RawFilterResults &= " and imageurl like '" & ImageDir & "%'"
        End If

        If CaratTextbox.Text > "" Then
            RawFilterResults &= " and carat = '" & CaratTextbox.Text & "'"
        End If

        If CutTextbox.Text > "" Then
            RawFilterResults &= " and cut = '" & CutTextbox.Text & "'"
        End If

        If ColorTextbox.Text > "" Then
            RawFilterResults &= " and color = '" & ColorTextbox.Text & "'"
        End If

        If ClarityTextbox.Text > "" Then
            RawFilterResults &= " and Clarity = '" & ClarityTextbox.Text & "'"
        End If

        If RawFilterResults.StartsWith(" and ") Then
            RawFilterResults = RawFilterResults.Substring(4)
        End If

        Return RawFilterResults

    End Function

【问题讨论】:

    标签: vb.net filter bindingsource


    【解决方案1】:

    使用流畅的风格界面。 Simple sample here

    或者更好的是使用 ORM,这样你就没有字符串编码的字段名称等

    【讨论】:

    • 感谢您的帮助。这绝对更易于使用和阅读。
    猜你喜欢
    • 2014-05-25
    • 1970-01-01
    • 2012-11-06
    • 2020-06-30
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    相关资源
    最近更新 更多