从一组控件[WinForms]构建BindingSource过滤器的更好方法

时间:2009-12-29 13:54:42

标签: vb.net filter bindingsource

现在我正在通过一堆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

1 个答案:

答案 0 :(得分:1)

使用流畅的样式界面。 Simple sample here

或者更好地使用和ORM,因此您没有字符串编码的字段名称等