Reputation: 51
I need an help.
I have a search tab on my program in vb.net. That works, but only shows me the information after i press the button, and i want to show me in same time as i wrote.
There is my code.
Public Sub search()
con.Open()
Dim dt As New DataTable
Dim ds As New OleDbDataAdapter("Select * from livrosescola where NomeLivro Like'%" & TextBox1.Text & "%'", con)
ds.Fill(dt)
DataGridView1.DataSource = dt.DefaultView
DataGridView1.Refresh()
ds.Dispose()
con.Close()
End Sub
If you didn't understand what i want, theres a link where you can see what i want. https://www.youtube.com/watch?v=WKY2RaZHi6Q > min 6:20 if you notice the datagridview refreshes at same time as he write.
Thank you.
Upvotes: 1
Views: 579
Reputation: 331
Actually you call your function search() in the button.Click something like:
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
search()
End Sub
Wath you need to do is use the KeyPress or TextChanged event of your TextBox1:
Public Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
search()
End Sub
On the other hand, execute a query for every key presed in your textbox could be a bit rough for you database.
What I suggest you (if your table doesn´t have much more than... say 50000 rows), it's fill a DataTable with all the rows and then when you need filter you do it over the DataTable instead of the database.
Upvotes: 2