使用CheckBox输入过滤ListView数据

时间:2016-11-18 23:45:33

标签: vb.net listview filter

我尝试使用ListView控件提供一种在CheckBox内过滤数据的方法。选中一个复选框将添加或删除ListView中的所选属性。

当前代码:

For i = 0 To RecCount
    Filter = True

    'RecordDat array populated here

    'filter the record by checkbox selection

    If ckbComplete.Checked = True And Len(RecordDat(7)) = 0 Then
        Filter = False
    ElseIf ckbOpen.Checked = True And Len(RecordDat(7)) > 0 Then
        Filter = false
    ElseIf ckbApps.Checked = True And strings.Left(RecordDat(0).ToString, 4) <> "APPS" Then
        Filter = False
    ElseIf ckbContract.Checked = True And Strings.Left(RecordDat(0), 3) <> "SOP" Then
        Filter = false
    End If

    If Filter = False Then GoTo SkipItem
    ' More code adding the item to the list view and formatting

SkipItem:
    Next

目前我每个数组索引只能过滤一次,即我可以过滤ckbCompleteckbApps,但不能过滤ckbcomplete, ckbAppsckbContract

1 个答案:

答案 0 :(得分:0)

我设法通过反复试验找出解决方案,并希望分享我找到的内容。

为了过滤数据,每个变量(在本例中为列值)都需要自己的过滤语句,每个语句都必须包含在If ELSE语句中。

结果代码如下:

Dim Filter As Boolean = True
        For i = 0 To RecCount
            Filter = False

'RecordDat initialisation               

            Filter = FilterPass(RecordDat)
            If Filter = False Then Continue For

            Dim itm As ListViewItem
            itm = New ListViewItem(RecordDat)
            Healthchart.ListView1.Items.Add(itm)

'Continue formatting and input of data into listview


        Next

然后FilterPass函数如下:

Private Function FilterPass(ByVal RecordDat() As String)
    Dim filter As Boolean = False
    If Healthchart.ckbComplete.Checked = True Then
        If Len(RecordDat(7)) > 0 Then
            filter = FilterPass2(RecordDat)
            If filter = True Then
                filter = FilterPass3(RecordDat)
            End If
        End If
    End If

    If Healthchart.ckbOpen.Checked = True Then
        If Len(RecordDat(7)) = 0 Then
            filter = FilterPass2(RecordDat)
            If filter = True Then
                filter = FilterPass3(RecordDat)
            End If
        End If
    End If
    Return filter
End Function


Private Function FilterPass2(ByVal RecordDat() As String)
    Dim filter As Boolean = False
    If Healthchart.ckbApps.Checked = True Then
        If Strings.Left(RecordDat(0).ToString, 4) = "APPS" Then
            filter = True
        End If
    End If
    If Healthchart.ckbContract.Checked = True Then
        If Strings.Left(RecordDat(0), 3) = "SOP" Then
            filter = True
        End If
    End If
    If Healthchart.ckbTech.Checked = True Then
        If Strings.Left(RecordDat(0), 3) = "ATS" Then
            filter = True
        End If
    End If
    If Healthchart.ckbDes.Checked = True Then
        If Strings.Left(RecordDat(0), 3) = "DES" Then
            filter = True
        End If
    End If
    Return filter
End Function

Private Function FilterPass3(ByVal RecordDat() As String)
    Dim filter As Boolean = True
    Dim SubDate As Date = Date.ParseExact(RecordDat(1).ToString, "dd/MM/yy", System.Globalization.DateTimeFormatInfo.InvariantInfo)

    If Healthchart.ckbLast30.Checked = True Then
        If DateAdd(DateInterval.Day, -30, Now) > SubDate Then
            filter = False
        End If
    End If
    If Healthchart.ckb7Days.Checked = True Then
        If DateAdd(DateInterval.Day, -7, Now) > SubDate Then
            filter = False
        End If
    End If
    Return filter
End Function

然后,可以在If Filter = true then函数中添加额外的FilterPass,根据需要添加尽可能多的“过滤器”。