Trying to code a "Search" Box

Tyecom

Well-known member
Joined
Aug 8, 2007
Messages
78
Programming Experience
Beginner
Do anyone know how to code a "search" textbox? I've been trying to do this for some time and can't get it done. Thanks in advance.
 
I created a datagrid and I want to be able to search the contents of this grid. Thats why I want some sort of search box.
 
Please be more specific.
How do you want the search to operate? If a user types something in the "search box" and clicks a button, should the datagrid then only show the search results or should the cell containing the search string be selected?
 
Hi Paszt,

If the user types something in the "search box" and clicks a button, I would like the datagrid to show the search result. Thanks in advance.
 
Dim i, i1 As Integer
Dim SearchString As String = Me.TextBox3.Text
Dim s As String
For i1 = 0 To Me.DataGridView1.Columns.Count - 1
For i = 0 To Me.DataGridView1.Rows.Count - 1
s = Me.DataGridView1.Item(i1, i).Value
If SearchString = s Then
MsgBox("Your Searched Text is in Column =" & i1 & " Row =" & i)

Exit Sub
End If
Next
Next
 
Thank you for responding. Can you please tell me "Where" to put this code (textbox, button, Public sub, Private sub, etc.) and how do I execute it. Also, can you provide a brief explanation of what the code is doing? I'm a new in the .Net environment.
 
why don't u use fillbysearch?
1. Click on the Datagridview
2. Go to Data -> Add Query
SELECT ColumnName1, ColumnName2 FROM TableName WHERE ColumnName1 = :parameterName

? for database
: oracle database
@ for access (i think)
 
RE: Trying to code a "Search" Box Reply to Thread

I'm doing the same thing and here's what I did. I hope it works for you and helps.

CONTROLS
cboFilter: (ComboBox) - contains these options:
Exactly Match
Similar Match
Starts with
Ends with

txtSearch: (Textbox)

dgMain: (DataGridView) : Contains data
dgMain.Datasource = dt

VB.NET:
Private Sub SearchRecords() 'Call this when the search button is clicked

Dim strFilter As String = cboFilter.SelectedItem.ToSTring
Dim strKeyword As String = txtSearch.Text
Dim strLogic As String = ""
Dim strLogic2 As String = ""
Dim strRowFilter As String = ""

Select Case strFilter
         Case "Exactly Match"
                 strLogic = "='"
                 strLogic2 = "'"
         Case "Similar Match"
                 strLogic = "LIKE '%"
                 strLogic2 = "%'"
         Case "Starts with"
                 strLogic = "LIKE '"
                 strLogic2 = "%'"
         Case "Ends with"
                 strLogic = "LIKE '%"
                 strLogic2 = "'"
End Select

'Put the columns you wish to search for in this logic
'In my case, I'm searching for firstname, lastname and city
strRowFilter = "FirstName " & strLogic & strKeyword & strLogic2 & _
                    "OR LastName " & strLogic & strKeyword & strLogic2 & _
                    "OR City " & strLogic & strKeyword & strLogic2

dt.DefaultView.RowFilter = strRowFilter

If dt.DefaultView.Count > 0 Then
 'found records
  dgMain.DataSource = dt.DefaultView
Else
 'no record so show all
  dt.DefaultView.RowFilter = ""
  dgMain.DataSource = dt
End If

End Sub
 
The solution would depend on how you are getting the data into the datagrid.
As I said, please be specific; present the problem clearly and you'll get better answers.
You posted in winforms and your profile currently states you're using .Net 2.0(VS2005) so are you using a dataGridView control?
What type of database? (doesn't matter that much in this situation, but when asking data access questions, it's best to state the database you're using).
Are you using a bindingSource?
Do you want to search all fields, just one, or several?
etc, ...

The best solution is to use the datagridview/bindingSource solution and simply set the Filter property of the bindingSource to include the column you want to search against equal to the textbox text. This would be done in a button click event handler.

See my post #14 in this thread for a walk through on setting up the dataGridView/bindingSource: Filter listbox items by textbox, Thread# 11626
 
Thanks Makavelli & Pastz. I will work both solutions and let you know if they worked. I apologize for not being clear, but I will work on that in the future. I am using SQL Server 2005 and using .Nets' DataGridView. I want to be able to retrieve information from two columns (Name and Address).
 
hi Tyecom !
just put that in button click
explanation
After filling u r datagrid
1. Enter u r text to be searched in textbox
2. click the button(put that coding wat i posted in this button_click event)
3.First it takes columns from 0 and continued to the end of the row(means keeping column 0 and navigates through the rows(0 to end )).
e.g assume columns 2 - rows 10
in my coding 'i1' increments only 2 times where as 'i' increments up to 10 times
each and every time entering into 'for loop of 'i' ' u r given search string will be matched with every single cell of a row , when u r given string matches with the cell values it executes the msgbox ,
r u clear now.

How it helped , reply
 
Back
Top