在为变量赋值之前使用变量,现在不过滤数据

时间:2018-08-09 12:29:24

标签: vb.net datatable

所以我有代码可以通过多个条件过滤数据表。其他所有条件都可以正常工作,但是这些条件基于文本框。所以代码就是

Dim SubjFilter As String = "Subjects = '" & TxtSubj.Text & " '"

,然后与下面的代码底部相同。这样就可以很好地打印列表框中的行,但是当我尝试使用单选按钮进行操作时,似乎找不到条件。在CSV中,第4列的每一行都有一条信息,该信息可以是75+或85+或类似的信息,所以我知道这不是问题。没有错误出现,这只是它无法找到该类别中任何内容的一个例子,或者我在列表框中将其打印的方式有问题。由于我现在迷路,任何见识都将受到赞赏。

Dim ATARFilter As String
    If RBtn70.Checked = True Or
    Rbtn75.Checked = True Or
    RBtn80.Checked = True Or
    Rbtn85.Checked = True Or
    Rbtn90.Checked = True Or
    Rbtn95.Checked = True Then 'if any of the radiobuttons are checked then the same printing process as before is done

        If RBtn70.Checked = True Then
            ATARFilter = "ATAR = '70+'"
        ElseIf Rbtn75.Checked = True Then
            ATARFilter = "ATAR = '75+'"
        ElseIf RBtn80.Checked = True Then
            ATARFilter = "ATAR = '80+'"
        ElseIf Rbtn85.Checked = True Then
            ATARFilter = "ATAR = '85+'"
        ElseIf Rbtn90.Checked = True Then
            ATARFilter = "ATAR = '90+'"
        ElseIf Rbtn95.Checked = True Then
            ATARFilter = "ATAR = '95+'" 'all of these just set the string value of the filter to be equal to whatever is on the RadioButton
            Dim FilteredRowsATAR As DataRow() = TutorTable.Select(ATARFilter)
            For Each row As DataRow In FilteredRowsATAR
                ListBox1.Items.Add(String.Format("{0},  {1},  {2},  {3},  {4},  {5}", row("Name"), row("Age"), row("Subjects"), row("ATAR"), row("Location"), row("PhNumber")))
            Next
        End If
    End If

2 个答案:

答案 0 :(得分:0)

这段代码需要Imports System.Linq。检查什么都不说什么都不检查

Private Sub OPCode()
            Dim ATARFilter As String = ""
            Dim rButton As RadioButton = GroupBox1.Controls.OfType(Of RadioButton).FirstOrDefault(Function(r) r.Checked = True)
            If rButton Is Nothing Then
                MessageBox.Show("Please make a selection.")
                Exit Sub
            Else
                ATARFilter = rButton.Text 'or if the exact text cannot not appear
                ' in the text of the radio button then store it in the .Tag property
                'at design time and change code to = CStr(rButton.Tag)
            End If
            Dim FilteredRowsATAR As DataRow() = TutorTable.Select(ATARFilter)
            For Each row As DataRow In FilteredRowsATAR
                ListBox1.Items.Add(String.Format("{0},  {1},  {2},  {3},  {4},  {5}", row("Name"), row("Age"), row("Subjects"), row("ATAR"), row("Location"), row("PhNumber")))
            Next
        End Sub

答案 1 :(得分:-1)

只有ElseIf中的最后一个条件包含应用过滤器的代码,您才需要将其移动到块外

Dim ATARFilter As String
    If RBtn70.Checked = True Or
    Rbtn75.Checked = True Or
    RBtn80.Checked = True Or
    Rbtn85.Checked = True Or
    Rbtn90.Checked = True Or
    Rbtn95.Checked = True Then 'if any of the radiobuttons are checked then the same printing process as before is done

        If RBtn70.Checked = True Then
            ATARFilter = "ATAR = '70+'"
        ElseIf Rbtn75.Checked = True Then
            ATARFilter = "ATAR = '75+'"
        ElseIf RBtn80.Checked = True Then
            ATARFilter = "ATAR = '80+'"
        ElseIf Rbtn85.Checked = True Then
            ATARFilter = "ATAR = '85+'"
        ElseIf Rbtn90.Checked = True Then
            ATARFilter = "ATAR = '90+'"
        ElseIf Rbtn95.Checked = True Then
            ATARFilter = "ATAR = '95+'" 'all of these just set the string value of the filter to be equal to whatever is on the RadioButton
        End If

        Dim FilteredRowsATAR As DataRow() = TutorTable.Select(ATARFilter)
        For Each row As DataRow In FilteredRowsATAR
            ListBox1.Items.Add(String.Format("{0},  {1},  {2},  {3},  {4},  {5}", row("Name"), row("Age"), row("Subjects"), row("ATAR"), row("Location"), row("PhNumber")))
        Next
    End If