shellee1983
asked on
How to search across multiple colums?
I am attempting to create a searchbox that allows a person to search for something across the columns. This works for a partial string but it does not move to the next record and I attempted to do a string array for my 7 column names with no avail. I looked on the internet for guidance and came up empty. Maybe someone here has written a similar function and can offer guidance. Thanks.
Private Function findPart(ByVal strSearch As String, ByVal colName As String) As Boolean
Dim Found As Boolean
Dim strToSearch As String = strSearch.Trim.ToUpper
Dim curRowInt As Integer = 0
dgViewClients.SelectionMod e = DataGridViewSelectionMode. FullRowSel ect
Try
If dgViewClients.Rows.Count = 0 Then
curRowInt = 0
Else
curRowInt = dgViewClients.CurrentRow.I ndex + 1
End If
If curRowInt > dgViewClients.Rows.Count Then
curRowInt = dgViewClients.Rows.Count - 1
End If
If dgViewClients.Rows.Count > 0 Then
For Each row As DataGridViewRow In dgViewClients.Rows
strToSearch = row.Cells(colName).Value.T oString.Tr im.ToUpper
If strToSearch.Contains(strSe arch) Then
Dim mCurrCell As DataGridViewCell =
row.Cells(colName)
dgViewClients.CurrentCell = mCurrCell
Found = True
End If
If Found Then
Exit For
End If
Next
End If
If Not Found Then
If dgViewClients.Rows.Count > 0 Then
Dim mFstCurCell As DataGridViewCell =
dgViewClients.Rows(0).Cell s(colName)
dgViewClients.CurrentCell = mFstCurCell
End If
End If
Catch ex As Exception
MsgBox("Error: " & ex.Message, MsgBoxStyle.Information Or MsgBoxStyle.OkOnly)
End Try
Return Found
End Function
Private Function findPart(ByVal strSearch As String, ByVal colName As String) As Boolean
Dim Found As Boolean
Dim strToSearch As String = strSearch.Trim.ToUpper
Dim curRowInt As Integer = 0
dgViewClients.SelectionMod
Try
If dgViewClients.Rows.Count = 0 Then
curRowInt = 0
Else
curRowInt = dgViewClients.CurrentRow.I
End If
If curRowInt > dgViewClients.Rows.Count Then
curRowInt = dgViewClients.Rows.Count - 1
End If
If dgViewClients.Rows.Count > 0 Then
For Each row As DataGridViewRow In dgViewClients.Rows
strToSearch = row.Cells(colName).Value.T
If strToSearch.Contains(strSe
Dim mCurrCell As DataGridViewCell =
row.Cells(colName)
dgViewClients.CurrentCell = mCurrCell
Found = True
End If
If Found Then
Exit For
End If
Next
End If
If Not Found Then
If dgViewClients.Rows.Count > 0 Then
Dim mFstCurCell As DataGridViewCell =
dgViewClients.Rows(0).Cell
dgViewClients.CurrentCell = mFstCurCell
End If
End If
Catch ex As Exception
MsgBox("Error: " & ex.Message, MsgBoxStyle.Information Or MsgBoxStyle.OkOnly)
End Try
Return Found
End Function
ASKER
Would something like below make it easier?
http://www.codeproject.com/Articles/9947/DataGrid-with-built-in-filter-functionality
That is a great concept however I am trying to limit the things the user has to do so basically I have one search box where they enter a partial string and the code now only can search one column at a time. I tried to do an array of the columns outside the function because I am using it as a partial public class between two forms but that broke it so back to the drawing board. I will try this and see what happens. I also need to see how to make it loop to the next match because that does not appear to be working.
http://www.codeproject.com/Articles/9947/DataGrid-with-built-in-filter-functionality
That is a great concept however I am trying to limit the things the user has to do so basically I have one search box where they enter a partial string and the code now only can search one column at a time. I tried to do an array of the columns outside the function because I am using it as a partial public class between two forms but that broke it so back to the drawing board. I will try this and see what happens. I also need to see how to make it loop to the next match because that does not appear to be working.
ASKER
I figured out how to do it with an array, now I just can't figure out how to get it to cycle through the searched partial string to highlight the next found row. Anyone have ideas?
Try
If dgViewClients.Rows.Count = 0 Then
curRowInt = 0
Else
curRowInt = dgViewClients.CurrentRow.I ndex + 1
End If
If curRowInt > dgViewClients.Rows.Count Then
curRowInt = dgViewClients.Rows.Count - 1
End If
If dgViewClients.Rows.Count > 0 Then
For Each row As DataGridViewRow In dgViewClients.Rows
'For Each cell As DataGridViewCell In row.Cells
For Each colName In colStr
strToSearch = row.Cells(colName).Value.T oString.Tr im.ToUpper
If strToSearch.Contains(strSe arch.ToUpp er) Then
Dim mCurrCell As DataGridViewCell =
row.Cells(colName)
dgViewClients.CurrentCell = mCurrCell
Found = True
End If
If Found Then
Exit For
End If
Next
Next
End If
'If Not Found Then
' If dgViewClients.Rows.Count > 0 Then
' Dim mFstCurCell As DataGridViewCell =
' dgViewClients.Rows(0).Cell s(colName)
' dgViewClients.CurrentCell = mFstCurCell
' End If
'End If
Catch ex As Exception
MsgBox("Error: " & ex.Message, MsgBoxStyle.Information Or MsgBoxStyle.OkOnly)
End Try
Return Found
End Function
Try
If dgViewClients.Rows.Count = 0 Then
curRowInt = 0
Else
curRowInt = dgViewClients.CurrentRow.I
End If
If curRowInt > dgViewClients.Rows.Count Then
curRowInt = dgViewClients.Rows.Count - 1
End If
If dgViewClients.Rows.Count > 0 Then
For Each row As DataGridViewRow In dgViewClients.Rows
'For Each cell As DataGridViewCell In row.Cells
For Each colName In colStr
strToSearch = row.Cells(colName).Value.T
If strToSearch.Contains(strSe
Dim mCurrCell As DataGridViewCell =
row.Cells(colName)
dgViewClients.CurrentCell = mCurrCell
Found = True
End If
If Found Then
Exit For
End If
Next
Next
End If
'If Not Found Then
' If dgViewClients.Rows.Count > 0 Then
' Dim mFstCurCell As DataGridViewCell =
' dgViewClients.Rows(0).Cell
' dgViewClients.CurrentCell = mFstCurCell
' End If
'End If
Catch ex As Exception
MsgBox("Error: " & ex.Message, MsgBoxStyle.Information Or MsgBoxStyle.OkOnly)
End Try
Return Found
End Function
Change this
dgViewClients.CurrentCell = mCurrCell
to
row.Selected = True
and remove
If Found Then
Exit For
End If
dgViewClients.CurrentCell = mCurrCell
to
row.Selected = True
and remove
If Found Then
Exit For
End If
ASKER
if it never exits the for will it have an infinite loop? I'm just curious because that's why I had the exit for in the first place. Also that code highlights all of them I was trying to think of a way to cycle through the next portion of it. as in:
if found = true then
mCurrCell.next (I know thats not the code but I am trying to figure that out lol)
end if
if found = true then
mCurrCell.next (I know thats not the code but I am trying to figure that out lol)
end if
Its not infinite loop. It runs for each row.
ASKER
I see the difference now I accidentally created an infinate loop by doing a do until.....it didn't end well. So now I just need to find how to iterate through the search and it will be golden. Thanks for your help thus far CodeCruiser.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The reason was that I found the information via a co worker and the solutions provided did not assist in any manner as I did not modify the code as per the suggestions as can be seen here:
Dim Found As Boolean
Dim strToSearch As String = strSearch.Trim.ToUpper
Dim curRowInt As Integer = 0
Dim colStr As Array
Dim colName As String
Dim i As Integer
'Dim findStr As Array
colStr = {"Report_Type", "Client_Number", "Client_Name", "Client_Type", "Transmission_Type", "Local", "TOA", "Contact_Person", "Phone_Number", "EMail_Address"}
dgViewClients.SelectionMod e = DataGridViewSelectionMode. FullRowSel ect
Found = False
If strLastSearch <> txtSearch.Text Then
iStartRow = 0
End If
Try
If dgViewClients.Rows.Count = 0 Then
curRowInt = 0
Else
curRowInt = dgViewClients.CurrentRow.I ndex + 1
End If
If curRowInt > dgViewClients.Rows.Count Then
curRowInt = dgViewClients.Rows.Count - 1
End If
If dgViewClients.Rows.Count > 0 Then
For i = iStartRow To dgViewClients.Rows.Count - 1
If Found = False Then
For Each colName In colStr
strToSearch = dgViewClients.Rows(i).Cell s(colName) .Value.ToS tring.Trim .ToUpper
If strToSearch.Contains(strSe arch.ToUpp er) Then
Dim mCurrCell As DataGridViewCell =
dgViewClients.Rows(i).Cell s(colName)
dgViewClients.CurrentCell = mCurrCell
Found = True
iStartRow = i + 1
Exit For
End If
Next colName
End If
Next i
End If
If Not Found Then
MsgBox("Searched Value " & txtSearch.Text & " Not Found. Please try again.", MsgBoxStyle.Information Or MsgBoxStyle.OkOnly)
End If
Catch ex As Exception
MsgBox("Error: " & ex.Message, MsgBoxStyle.Information Or MsgBoxStyle.OkOnly)
End Try
Return Found
End Function
Dim Found As Boolean
Dim strToSearch As String = strSearch.Trim.ToUpper
Dim curRowInt As Integer = 0
Dim colStr As Array
Dim colName As String
Dim i As Integer
'Dim findStr As Array
colStr = {"Report_Type", "Client_Number", "Client_Name", "Client_Type", "Transmission_Type", "Local", "TOA", "Contact_Person", "Phone_Number", "EMail_Address"}
dgViewClients.SelectionMod
Found = False
If strLastSearch <> txtSearch.Text Then
iStartRow = 0
End If
Try
If dgViewClients.Rows.Count = 0 Then
curRowInt = 0
Else
curRowInt = dgViewClients.CurrentRow.I
End If
If curRowInt > dgViewClients.Rows.Count Then
curRowInt = dgViewClients.Rows.Count - 1
End If
If dgViewClients.Rows.Count > 0 Then
For i = iStartRow To dgViewClients.Rows.Count - 1
If Found = False Then
For Each colName In colStr
strToSearch = dgViewClients.Rows(i).Cell
If strToSearch.Contains(strSe
Dim mCurrCell As DataGridViewCell =
dgViewClients.Rows(i).Cell
dgViewClients.CurrentCell = mCurrCell
Found = True
iStartRow = i + 1
Exit For
End If
Next colName
End If
Next i
End If
If Not Found Then
MsgBox("Searched Value " & txtSearch.Text & " Not Found. Please try again.", MsgBoxStyle.Information Or MsgBoxStyle.OkOnly)
End If
Catch ex As Exception
MsgBox("Error: " & ex.Message, MsgBoxStyle.Information Or MsgBoxStyle.OkOnly)
End Try
Return Found
End Function
>strToSearch = row.Cells(colName).Value.T
To search across all columns, you would need to use an inner loop similar to below
For Each Cell As DataGridViewCell In row.Cells
...
Next
Would something like below make it easier?
http://www.codeproject.com/Articles/9947/DataGrid-with-built-in-filter-functionality