在 vb.net 中使用文本框从 datagridview 中搜索数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25013249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:02:09  来源:igfitidea点击:

Search data from datagridview using textbox in vb.net

vb.net

提问by codeapp17

I have a Textbox and i want that when we enter something in Textbox,it will search that data from Datagridview . I searched lot on this but find that search is gone through database but i want that search is gone from the datagridview.

我有一个文本框,我希望当我们在文本框中输入内容时,它会从 Datagridview 搜索该数据。我对此进行了大量搜索,但发现搜索已通过数据库进行,但我希望该搜索从 datagridview 中消失。

回答by Suji

The following code will search for the text in the text box is present or not in datagridview @ any cell in the grid( search the whole grid)

以下代码将在datagridview@网格中的任何单元格中搜索文本框中的文本是否存在(搜索整个网格)

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim temp As Integer = 0
    For i As Integer = 0 To gv.RowCount - 1
        For j As Integer = 0 To gv.ColumnCount - 1
            If gv.Rows(i).Cells(j).Value.ToString = TextBox1.Text Then
                MsgBox("Item found")
                temp = 1
            End If
        Next
    Next
    If temp = 0 Then
        MsgBox("Item not found")
    End If
End Sub

回答by Bj?rn-Roger Kringsj?

The following examples, using language-integrated query (LINQ), will test the formatted value of each cell in the grid to the given condition and return an array of matched cells.

以下示例使用语言集成查询 (LINQ)将测试网格中每个单元格的格式化值是否符合给定条件并返回匹配单元格的数组。

Exact match(a = b)

完全匹配(a = b)

Dim match As DataGridViewCell() = (From row As DataGridViewRow In Me.DataGridView1.Rows From cell As DataGridViewCell In row.Cells Select cell Where CStr(cell.FormattedValue) = Me.TextBox1.Text).ToArray()

Pattern match(a LIKE %b%)

模式匹配(一个 LIKE %b%)

Dim match As DataGridViewCell() = (From row As DataGridViewRow In Me.DataGridView1.Rows From cell As DataGridViewCell In row.Cells Select cell Where CStr(cell.FormattedValue).Contains(Me.TextBox1.Text)).ToArray()