DataGridView结果

时间:2016-05-04 11:51:29

标签: .net vb.net datagridview

我正在尝试在VB.net中构建搜索功能。我的意图是将字符串输入切断并根据这些子字符串返回结果。问题是我得到了同一件事的多个副本。因此我为什么添加下面的代码。例如,如果我搜索“wifi不工作”,我会得到同一件事的三个副本,我只想要一个相同结果的副本,但我想保持搜索子字符串的灵活性。请协助。

 For Each rowy As DataGridViewRow In DataGridView1.Rows
                            If element(0).ToString = rowy.Cells(0).Value.ToString Then
                            Else
                                DataGridView1.Rows.Add(element(0).ToString)
                            End If

下一步

Public Class Form1
    Dim searchQuery As String
    Dim table = New DataTable
    Dim words() As String

    'Store String, segregate string into list
    Private Sub SearchBttn_Click(sender As Object, e As EventArgs) Handles SearchBttn.Click
        DataGridView1.Rows.Clear()
        searchQuery = SearchBox.Text
        words = searchQuery.Split(TryCast(Nothing, String()), StringSplitOptions.RemoveEmptyEntries)
        SearchResults(words)
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        table.Columns.Add("result")
        table.Rows.Add("Unlock LAN account")
        table.Rows.Add("Wifi Not Working")
        table.Rows.Add("Account Error - 1230")

    End Sub


    Private Sub SearchResults(value As Array)
        For Each element As DataRow In table.Rows
            For i As Integer = 0 To value.Length - 1
                Dim upperElem As String = element(0).ToString.ToUpper
                Dim upperVal As String = value(i).ToString.ToUpper
                If (upperElem.Contains(upperVal)) Then
                    If DataGridView1.RowCount = 0 Then
                        DataGridView1.Rows.Add(element(0).ToString)
                    Else
                        For Each rowy As DataGridViewRow In DataGridView1.Rows
                            If element(0).ToString = rowy.Cells(0).Value.ToString Then
                            Else
                                DataGridView1.Rows.Add(element(0).ToString)
                            End If
                        Next

                    End If
                End If
            Next
        Next
        Array.Clear(words, 0, words.Length)
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

您应该使用BindingSource,将DataSource设置为DataTable并将其绑定到DataGridView.DataSource。这使事情变得更容易。

此外,您可以使用Linq避免SearchResults方法中的大量代码。

事先在字符串上使用ToUpper已弃用,应由设置Option Comapre Text或使用"a".Equals("A",StringComparison.InvariantCultureIgnoreCase)等案例不变比较替换。

这是我的重写代码,应该给你一个良好的开端:

Option Comapre Text
... 

Dim bs As New BindingSource
...

'Store String, segregate string into list
Private Sub SearchBttn_Click(sender As Object, e As EventArgs) Handles Button1.Click      
    searchQuery = SearchBox.Text
    words = searchQuery.Split(TryCast(Nothing, String()), StringSplitOptions.RemoveEmptyEntries)
    SearchResults(words)
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    table.Columns.Add("result")
    table.Rows.Add("Unlock LAN account")
    table.Rows.Add("Wifi Not Working")
    table.Rows.Add("Account Error - 1230")
    bs.DataSource = table
    DataGridView1.DataSource = bs     'Remove this line if the grid should be empty first 
End Sub

Private Sub SearchResults(ParamArray values As String())
    Dim filteredRows = table.Rows.Cast(Of DataRow).Where(Function(r) values.Any(Function(s) r(0).ToString.Contains(s))) 'Find any row which contains any of the given filter words
    If(filteredRows.Any)
        bs.DataSource = filteredRows.CopyToDataTable 'Bind grid to found rows
    Else
        bs.DataSource = table 'Reset grid to show all rows. Remove this if grid should be empty if nothing is found
    End If
    bs.ResetBindings(False)
End Sub

重要提示:由于输入字符串未经过滤使用,因此您和您的代码非常容易受到SQL注入攻击。在处理过滤之前,您应该从搜索字符串中删除或转义任何恶意字符串(假设当前填充的DataTable仅用于调试,稍后您将使用SQL)。