使用BindingListView进行多列过滤

时间:2013-02-08 14:57:46

标签: .net search datagridview filtering bindinglist

我正在使用位于http://blw.sourceforge.net/的BindingListView dll来使用Generic List对DataGridView进行排序,因为它是DataSource。我读过有关过滤的内容,使用起来相当简单,但我在尝试弄清楚如何使用多个搜索参数进行过滤时遇到了问题。这是一个例子......

Private Sub txtProjectNumber_TextChanged(sender As Object, e As System.EventArgs) Handles txtProjectNumber.TextChanged, txtDescription.TextChanged,
    txtOracleNumber.TextChanged, txtBudgetYearFrom.TextChanged, txtBudgetYearTo.TextChanged, txtWeek3Start.TextChanged, txtWeek3End.TextChanged
    view.ApplyFilter(AddressOf FilterData)
End Sub

Private Function FilterData(ByVal projectDetails As ProjectDetails) As Boolean
    Try
        If Not String.IsNullOrWhiteSpace(txtProjectNumber.Text) Then  
            Return projectDetails.ProjectNum.ToLower().StartsWith(txtProjectNumber.Text.ToLower())
        End If
        If Not String.IsNullOrWhiteSpace(txtDescription.Text) Then
            Return projectDetails.Description.ToLower().StartsWith(txtDescription.Text.ToLower())
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
    Return False
End Function

所以在这里你可以看到,如果我更改txtProjectNumber TextBox中的文本,我的过滤器将返回正确的结果。但是,如果我说txtProjectNumber TextBox中的“x”并且想要通过txtDescription“morning”进行搜索,则会忽略txtDescription,因为它会命中txtProjectNumber并返回,而不会访问txtDescription TextBox。如何让它继续下去,从每个非空输入构建一个过滤器?

1 个答案:

答案 0 :(得分:0)

您只需要根据测试返回一个布尔值,您实际上并没有返回对象本身,而是多次返回只是将您的条件放在if / andalso语句中,如果满足所有条件,则返回true。 / p>

Private Function FilterData(ByVal projectDetails As ProjectDetails) As Boolean
    Dim result As Boolean = False

    If projectDetails.ProjectNum.ToLower().StartsWith(txtProjectNumber.Text.ToLower()) andalso _
       projectDetails.Description.ToLower().StartsWith(txtDescription.Text.ToLower()) andalso _

              <remaining tests>

       result = true
    End If
Return result
End Function