简体   繁体   English

如何过滤DataGridView并进行管理?

[英]How to filter DataGridView and manage it?

I am new to VB.Net and Window Forms. 我是VB.Net和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. 在此过滤之后,将显示表“PersonInfos”中的所有信息(所有列)。 If I just want the column of "Last Name", "Age" and "Nationality" from the table (in a .mdb file) be shown only. 如果我只想显示表中的“姓氏”,“年龄”和“国籍”列(在.mdb文件中),则只显示。 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: 这是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

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM