Question

I am new to VB.Net and Window Forms. I am doing a filtering on a table, so that the words entered in searching machine can be used to show the infos(row) that are relevant only. I will explain my situation after I showed my codes:

Private Sub findTextBox2_TextChanged(sender As Object, e As EventArgs) Handles findTextBox2.TextChanged

        Dim viewPerson As DataView
        viewPerson = New DataView(Data.ds.Tables("PersonInfos"))

        If Not String.IsNullOrEmpty(Me.findTextBox2.Text) Then
            Dim person As String = FilterListBox("PersonInfos", Me.findTextBox2.Text)
            viewPerson.RowFilter = "ID in (" & person & ") "
        Else
            viewPerson.RowFilter = ""
        End If

        DataGridView2.DataSource = viewPerson 

        con.Close()

    End Sub

After this filtering, all the infos (all the columns) frm the table "PersonInfos" will be showed. If I just want the column of "Last Name", "Age" and "Nationality" from the table (in a .mdb file) be shown only. What should i include in the codes.

I have search online and didn't manage to find a solution to my problem. Any help will be much appreciated. Thank you in advance.

This is what inside the method FilterListBox:

Private Function FilterListBox(ByVal TableName As String, ByVal tbText As String) As String

        Dim IDs As String = String.Empty
        If tbText.Contains("*") Then

            If Not tbText.StartsWith("*") Then
                tbText = "^" + tbText
            End If

            tbText = tbText.Replace("*", ".*")
            If tbText.Contains("?") Then
                tbText = tbText.Replace("?", ".")
            End If

            For Each row As DataRow In data.ds.Tables(TableName).Rows
                If System.Text.RegularExpressions.Regex.IsMatch(row.Item("Name"), tbText, System.Text.RegularExpressions.RegexOptions.IgnoreCase) Then
                    If IDs <> "" Then IDs &= ","
                    IDs &= row.Item("ID")
                    Dim dfd As String = row.Item("Name").ToString()

                    Dim a As Boolean = System.Text.RegularExpressions.Regex.IsMatch(row.Item("Name"), tbText, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
                End If
            Next
        Else
            For Each row As DataRow In data.ds.Tables(TableName).Rows
                If row.Item("Name").ToString.StartsWith(tbText, StringComparison.CurrentCultureIgnoreCase) Then
                    If IDs <> "" Then IDs &= ","
                    IDs &= row.Item("ID")
                    Dim dfd As String = row.Item("Name").ToString()

                    Dim a As Boolean = System.Text.RegularExpressions.Regex.IsMatch(row.Item("Name"), tbText, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
                End If
            Next
        End If
        If IDs <> "" Then
            Return IDs
        Else
            Return "1=2"
        End If

    End Function
Était-ce utile?

La solution

Try this

Dim viewPerson As DataView
Dim da As New OleDbDataAdapter 
Dim tbl as New DataTable


da = New OleDbDataAdapter("SELECT Last Name, Age, Nationality FROM PersonInfos", con)       
da.Fill(tbl)

viewPerson = New DataView(tbl)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top