如何过滤 DataGridView 中的过滤数据

周杰伦

我在过滤数据网格视图中的数据时遇到问题,如果我使用 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-filter 情况下,它将满足此条件If Not String.IsNullOrEmpty(Me.Court_AgencyComboBox.Text)并因此执行该行。但是,在您的 2-filter 情况下,它将检查第一个条件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...)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章