我在数据网格视图中过滤数据时遇到问题,如果我使用1个搜索过滤器,我的代码可以工作,但是如果我使用2个搜索过滤器,则我的代码不起作用,首先我需要按法院进行过滤,然后再按场所进行过滤或位置
请告诉我有关如何使此代码有效的想法,谢谢
我的代码:
Private Sub Searchbtn_Click(sender As Object, e As EventArgs) Handles Searchbtn.Click
'Search Code
If Me.Court_AgencyComboBox.Text = "" Then
MessageBox.Show("No Item to be Search", "IBP Legal Aid Case Management System - Legal Aid Case File - Search", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
If Not String.IsNullOrEmpty(Me.Court_AgencyComboBox.Text) Then
Me.TblLegalAidCaseFileBindingSource.Filter = String.Format("[Court/Agency] like '%{0}%'", Me.Court_AgencyComboBox.Text)
ElseIf Not String.IsNullOrEmpty(Me.Court_AgencyComboBox.Text) And Not String.IsNullOrEmpty(Me.VenueComboBox.Text) Then
Me.TblLegalAidCaseFileBindingSource.Filter = String.Format("[Court/Agency] like '%{0}%' and Venue like '%{0}%'", Me.Court_AgencyComboBox.Text, Me.VenueComboBox.Text)
Else
RefreshData()
Return
End If
End Sub
if-else语句的工作方式是它将执行符合条件的第一个语句。在1过滤器的情况下,它将满足此条件If Not String.IsNullOrEmpty(Me.Court_AgencyComboBox.Text)
,从而执行该行。但是,在您的2过滤器情况下,由于该条件实际上已满足,因此它将检查第一个条件If Not String.IsNullOrEmpty(Me.Court_AgencyComboBox.Text)
并也执行该行。
直接解决问题的一种简便方法是交换条件。从最难满足的条件开始。看起来像这样(我嵌套了您的条件以使其更整洁):
(Code before...)
If Not String.IsNullOrEmpty(Me.Court_AgencyComboBox.Text) Then
If Not String.IsNullOrEmpty(Me.VenueComboBox.Text) Then
Me.TblLegalAidCaseFileBindingSource.Filter = String.Format("[Court/Agency] like '%{0}%' and Venue like '%{1}%'", Me.Court_AgencyComboBox.Text, Me.VenueComboBox.Text)
Else
Me.TblLegalAidCaseFileBindingSource.Filter = String.Format("[Court/Agency] like '%{0}%'", Me.Court_AgencyComboBox.Text)
Else (Code after...)