我有一个文本框,我希望当我们在Textbox中输入内容时,它会从Datagridview中搜索该数据。我搜索了很多,但发现搜索已经通过数据库,但我希望搜索从datagridview消失。
答案 0 :(得分:3)
以下代码将搜索文本框中的文本是否存在于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
答案 1 :(得分:2)
以下示例使用language-integrated query (LINQ),将网格中每个单元格的格式化值测试到给定条件,并返回匹配单元格的数组。
完全匹配(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()
模式匹配(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()