search to the datagrid w/ the use of textbox value.


Well-known member
Jun 3, 2004
Other side of the rock
Programming Experience
hi to everyone,
I have this problem. how can i search to the datagrid w/ the use of textbox value?
f.e. when the user input name in the textbox and press the enterkey i want to get focus to the datagrid of what value that are in textbox.

thanks in advance...
I would also like to know

I would also like to know.

In principle, I figured, you should perform the operation on the dataset and not the datagrid, since the dataset contains all the data and the datagrid is only an interface between the data and the user.

I haven't figured it out yet, but I'll get back to you if/when I do.
i surrender in this matter...
i only use listview to fill my data.
coz i have a solution in searching to the listview..hehehe

but anyways thanks in advance if you give the solution of this..

cheers mate..
Grinding the grid

I know how to find a value inside a dataset.

Dataset.tablename.Rows.Find(primary key value)

This finds a row in the dataset according to the provided primary key value.
If you use a textbox to enter the primary key, then it's pretty much self-explanitory. Otherwise, if you search according to a non-primary key value, you'll need to construct a command-object (with appropriate sql-statements) which searches the Table for the entered text in the specified column, and then returns the corresponding primary-key value. You then have to read that value into a variable, and then pass it as a parameter to the above mentioned statement. (You can actually pass it directly without reading that value into a variable first, but let's keep it as simple as possible).

Whether this will automatically set the focus on the correct record in the datagrid, I don't know. I still haven't tried or tested this theory myself, so I don't have any sample code to offer. I'm sure that this will at least take you to the point where the correct record inside the dataset is found. If it doesn't automatically set the focus to that record, look for some function of the datagrid to refresh or something.

Anyways, I hope this helps (or at least makes sense).

Let me know...
Last edited:
Try it

I reckon you should take the value of the field you're searching for, create a command object, construct a SELECT-query (that selects all the records with fields that correspond with that value) inside the constructor of the command object, execute the command object and refresh the listview-object.

That's what my gut-feeling tells me, but it might be wrong. Then again, it might be right. Try it and tell me if it worked.
Last edited:
this what i've done in my listview
i dont think if it is the efficient one.
 Option Compare Text
 'in the formload
 Dim dt As New DataTable()
 		Dim da As New SqlDataAdapter("select * from customers order by companyname asc", cn)
 		Dim dr As DataRow
 		For Each dr In dt.Rows
 			Dim li As ListViewItem = ListView1.Items.Add(dr(1))
 'in the function search
  Sub search(ByVal s As String)
 		Dim li As ListViewItem
 		For Each li In ListView1.Items
 			li.BackColor = Color.White
 		If s = "" Then
 			Exit Sub
 		End If
 		For Each li In ListView1.Items
 			If li.SubItems(0).Text Like (s + "*") Then
 				li.BackColor = Color.LightGray
 			End If
 	End Sub

just try it.

Simply use a searching algorithm.. as you would know that datagrid is a 2 -dimensional array.After the searching....get the cell position of datagrid from the search result and bring it to Focus.

If still there's any confusion.. let me know ...I'll write u some Code.