Search “text” within DataGridView

longbow2000

New member
Joined
Jul 30, 2009
Messages
4
Programming Experience
Beginner
Hi
How would you go about to search for specific “first, second, etc” string characters within a cell’s text? :confused:

The deal is:
I have a populated DataGrid and text box on Form1:
DataGrid Column 1’s data per row (Ignore the “” characters):

“John Smith”
“Jake White”
“Baby Boom”
“John Brown”
etc

If the user type “j” / “J” in the textbox (on Form1) then the DataGrid must filter the rows only to show the “string” that contains “j” / “J”, i.e.
“John Smith”
“Jake White”
“John Brown”

If the user type a second characters in the textbox it must filter it further (“jo” / “JO”) then the DataGrid must filter the rows only to show the “string” that contains “jo” / “JO”, i.e.
“John Smith”
“John Brown”

Thanks in advance
 
A DataGrid is a control for displaying connected data. Its important to realize that you want to work with the data itself, not actually the datagrid. There for you want to search the source of the data whether thats a dataset or datatable

VB.NET:
DataGridView1.DataSource = myDataset.myTable

Private Sub FilterData(strName as string)

    myDataset.myTable.DefaultView.RowFilter = String.Format("ColumnName Like '{0}%'", strName.Trim)

End Sub
 
Well actually I found out how to do it, I used an sql query to filter out the results I didn't want...
VB.NET:
SELECT ID, Frame, [Stock Num],  Color FROM dbo.Materials WHERE Frame like '%'+@search+'%' or [Stock Num] like '%'+@search+'%' or Color like '%'+@search+'%'


However, because of the structure of this statement, it will only look in one column at a time. For example, if there is a frame called bob with a color brown, and i type in bob brown and filter results, it will not show up with anything, because Frame does not equal bob brown, and color does not equal bob brown, and stock num does not equal bob brown, even though the frame = bob and the color = brown.

How can I formulate this statement so that it searches all columns for the text specified, and would do what I just stated above?

Thanks!
 
Last edited:

Latest posts

Back
Top