-
Sep 9th, 2012, 02:26 AM
#1
Thread Starter
New Member
Auto search through textbox and dynamically populate datagrid
hello everyone
can anyone help on this.
i have a table with column's (itemcode, itemname, price) on the vb form i have one search field (textbox1) and datagrid
i have data as
itemcode itemname price
1234 apple 10
1235 orange 12
1345 grapes 15
1356 banana 15
1357 apricot 25
2345 strawberry 20
2346 guava 10
now what i am trying is if i type 1 then grid show all data's starting with 1 and as i type 12 then it should filter only remaining data with itemcode starting with 12... and so on.\
i hope i am able to get my point through..
simple search i am able to i.e. when whole string match's with itemcode it shows the details in gridview.
code i m using on change event of textbox is
Code:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
'i have opened connection above
cn.Open()
Dim a As New OleDbDataAdapter("Select * from Item where ItemName = ' " & TextBox1.Text & "'", cn)
Dim b As New DataSet
a.Fill(b, "Item")
DataGridView2.DataSource = b.Tables(0)
cn.Close()
End Sub
thanx in advance
-
Sep 9th, 2012, 02:42 AM
#2
Re: Auto search through textbox and dynamically populate datagrid
If you want to make partial matches then you have to use the LIKE operator rather than =, and you must couple that with one or more wildcards. It appears that you want to perform a "starts with" search, so that means using a wildcard at the end of the input value that will match zero or more characters. In this and EVERY other case, you should also be using parameters, which will, among other things, avoid the error that you have made by putting a spurious space in your value.
vb.net Code:
Dim adapter As New OleDbDataAdapter("SELECT * FROM Item WHERE ItemName LIKE @ItemName", connection) adapter.Parameters.AddWithValue("@ItemName", TextBox1.Text & "%")
By the way, if all you're using is a single DataTable then using a DataSet is pointless. Just create a DataTable.
-
Sep 9th, 2012, 03:05 AM
#3
Thread Starter
New Member
Re: Auto search through textbox and dynamically populate datagrid
sorry, i have actually just started with vb.net and dont know use of parameter's.
anyway i m getting following error if i use your code.
could you please explain code without parameter's
Thanx
-
Sep 9th, 2012, 03:43 AM
#4
Re: Auto search through textbox and dynamically populate datagrid
Soryy, that should be:
Code:
adapter.SelectCommand.Parameters
-
Sep 9th, 2012, 07:23 AM
#5
Thread Starter
New Member
Re: Auto search through textbox and dynamically populate datagrid
Sorry it doesn't work, gives following error
and just a gentle reminder that values in textbox1 are numeric, if that's causing any problem..
thanx
-
Sep 9th, 2012, 07:53 PM
#6
Re: Auto search through textbox and dynamically populate datagrid
Um, you've left off the AddWithValue. I meant that it should have been 'adapter.SelectCommand.Parameters' rather than just 'adapter.Parameters'. Parameters is a property of each command and each command is a property of the data adapter. AddWithValue is a method of the parameter collection that allows you to add a parameter with a value to the parameter collection of the adapter's SELECT command.
-
Sep 10th, 2012, 02:08 AM
#7
Thread Starter
New Member
Re: Auto search through textbox and dynamically populate datagrid
Thanx dear
Finally i got it, i happen to do one tutorial with parameters. that's how got hold of it..
Cheers
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|