我的winform中有一个文本框和一个datagridew。dgvw通过代码连接到一个sql数据库。我添加了一个代码来搜索textbox的textchange事件中的datagridview。问题是,假设我在文本框,dgvw立即显示结果,但在我完成搜索后,我用退格键清除文本框,当我点击表格上的任何按钮时,dgvw变空了!没有行或列。但是,当我点击文本框并键入内容时,它们会再次出现......我该怎么办?
Private Sub OffPo_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Dim cmd As New SqlCommand("select * from userandadmins", con)
Dim adapter As New SqlDataAdapter(cmd)
Dim table As New DataTable
adapter.Fill(table)
userdatagrid.DataSource = table
Dim imm As New DataGridViewImageColumn
imm = userdatagrid.Columns(0)
imm.ImageLayout = DataGridViewImageCellLayout.Stretch
userdatagrid.Columns(0).Width = 35
Catch ex As Exception
End Try
Private Sub filterdata(valuetosearch As String)
Dim srq As String = "Select * from userandadmins where CONCAT(Title,FirstName,LastName,Phone,Mobile,Email,AccountType,Username,Password) like '%" & valuetosearch & "%' "
Dim mycmd As New SqlCommand(srq, con)
Dim adapter As New SqlDataAdapter(mycmd)
Dim table As New DataTable
adapter.Fill(table)
userdatagrid.DataSource = table
End Sub
Private Sub textbox1_textchanged
filterdata(textbox1.text)
End Sub