2013-02-08 204 views
2

我使用位於http://blw.sourceforge.net/的BindingListView DLL來啓用對具有通用列表的DataGridView進行排序,因爲它是DataSource。我閱讀過濾,它使用起來相當簡單,但我在試圖弄清楚如何使用過濾器的多個搜索參數時遇到問題。下面是一個例子...使用BindingListView進行多列篩選

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文本框中的文本我過濾器將返回正確的結果。但是如果我在txtProjectNumber TextBox中放置了「x」,並且還希望通過「morning」的txtDescription進行搜索,則txtDescription會被忽略,因爲它會觸擊txtProjectNumber並返回,而不會觸及txtDescription文本框。我怎樣才能讓它繼續下去,從每個非空輸入建立一個過濾器?

回答

0

您只需要根據測試返回一個布爾值,但實際上並不是返回對象本身,而是返回多個返回值,只需將條件放入if/andalso語句中,並在滿足所有條件的情況下返回true。

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