I have a datagrid and it contains a records from the dataset. How do I search a record on it and if the record is found, It will point to the said record.
Here is a code sample that will loop through a column in a datagrid and highlight a match (or the next match) everytime btnFind is clicked. The user will first need to click somewhere in the column they want to perform the search in.
There are 3 controls in this example.
btnFind (button), txtFind (textbox) and dgrdProductIDs (datagrid). In addition, this search logic requires a table style be applied to the datagrid, in this example the table style is called ts.
Also, these variables need to be declared at the module level
Private colToSearch As Int16
Private strColToSearch As String
Private ts As New DataGridTableStyle
Private aryRowsAlreadyFound(0) As Int16
Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
Dim strToFind As String = txtFind.Text
Dim strFilter As String
Dim rSelect() As DataRow
Dim r1 As DataRow
Dim r2 As DataRow
Dim intRowNumber As Int32
If strToFind = "" Then
Exit Sub
End If
Cursor = Cursors.WaitCursor
If InStr(strToFind, "%") > 0 Then
'has wildcard(s)
strFilter = strColToSearch & " Like " & "'" & strToFind & "'"
Else
'does not have any wildcards
strFilter = strColToSearch & " = " & "'" & strToFind & "'"
End If
'rSelect contains all the rows where a match was found
rSelect = ds.Tables(0).Select(strFilter)
If rSelect.Length > 0 Then
'loop through the main dataset and
'highlight the cell that matches
For Each r1 In ds.Tables(0).Rows
For Each r2 In rSelect
If UCase(r1.Item(strColToSearch).ToString) = UCase(r2.Item(strColToSearch).ToString) Then
'check the array that holds the row numbers
'already found
'if this row has already been found,
'don't find it again
Dim i As Int32
For i = 1 To UBound(aryRowsAlreadyFound)
If aryRowsAlreadyFound(i) = intRowNumber Then
GoTo skipthisone
End If
Next
'the colToSearch variable
'was set when the user clicked
'the column in the datagrid
dgrdProductIDs.CurrentCell = New DataGridCell(intRowNumber, colToSearch)
'this array stores the rows already found
'increase the size of the array by 1
Dim intNewLength As Int32 = aryRowsAlreadyFound.Length + 1
ReDim Preserve aryRowsAlreadyFound(intNewLength)
'add the last row found to the array
aryRowsAlreadyFound(intNewLength) = intRowNumber
'once a match has been found
'you need to get out of this sub
Cursor = Cursors.Default
Exit Sub
End If
skipthisone:
Next
'advance i for the next loop
intRowNumber += 1
Next
Else
Cursor = Cursors.Default
MsgBox("No matches have been found.", vbOKOnly, "END OF SEARCH")
End If
Cursor = Cursors.Default
If ds.Tables(0).Rows.Count > 0 Then
If intRowNumber = ds.Tables(0).Rows.Count Then
MsgBox("You have reached the end of the search.", vbOKOnly, "END OF SEARCH")
End If
End If
End Sub
Private Sub dgrdProductIDs_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgrdProductIDs.MouseUp
btnFind.Enabled = True
Dim pt = New Point(e.X, e.Y)
Dim hti As DataGrid.HitTestInfo = dgrdProductIDs.HitTest(pt)
If (hti.Type = DataGrid.HitTestType.Cell) Then
'get the column header
strColToSearch = (dgrdProductIDs.TableStyles(0).GridColumnStyles.Item(hti.Column).HeaderText.ToString())
ElseIf (hti.Type = dgrdProductIDs.HitTestType.ColumnHeader) Then
'get the column header
strColToSearch = (dgrdProductIDs.TableStyles(0).GridColumnStyles.Item(hti.Column).HeaderText.ToString())
End If
'get the column number
colToSearch = hti.Column
'clear out the array of found rows
'to get ready for a new search
ReDim aryRowsAlreadyFound(0)
i have problem with the scrolling part because i find and select the row i want according to the search but it doesnt scroll to where the selected row is. i have no idea how to work this out. hope you can help. thanx
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.