3

I have a data grid view in one windows form named "GridViewForm". When the user search for the text from the search box from another window form named "FindForm", I want to highlight all the matching result in the data grid view. The search type can be exact or partial.

For eg.

If the user search for the text "stack", then the words "stack" from [Stack, stack-over, stacks, stack exchange] should be highlighted and first cell that match the query should be selected. When the user press next button then another cell that match the search query should be selected.

My code for finding the text is like follow for it search only the exact word.

        Dim gridRow As Integer = 0
        Dim gridColumn As Integer = 0
        For Each Row As DataGridViewRow In AccountsDataGridView.Rows
            For Each column As DataGridViewColumn In AccountsDataGridView.Columns
                If TryCastString(AccountsDataGridView.Rows(gridRow).Cells(gridColumn).Value).ToLower = SearchTextBox.Text.ToLower Then
                    'AccountsDataGridView.Rows(intcount).Cells(0).Value = "0"
                    MsgBox("FOUND") 'Should be highlight insted of showing message and the cell should be select.
                End If
                gridColumn += 1
            Next column
            gridColumn = 0
            gridRow += 1
        Next Row

Is there any way to implement my concept? I am using vb.net windows form. Thanks in advance.

nightfire001
  • 759
  • 3
  • 20
  • 50

3 Answers3

4

You could use String.contains instead of an =.

Here is the MSDN article on the contains method:

http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx

code to style the cell if it contains the search text:

    Dim someText As String = SearchTextBox.Text
    Dim gridRow As Integer = 0
    Dim gridColumn As Integer = 0
    For Each Row As DataGridViewRow In AccountsDataGridView.Rows
        For Each column As DataGridViewColumn In AccountsDataGridView.Columns
            Dim cell As DataGridViewCell = (AccountsDataGridView.Rows(gridRow).Cells(gridColumn))
            If cell.Value.ToString.ToLower.Contains(someText.ToLower) Then
                cell.Style.BackColor = Color.Yellow
            End If
            gridColumn += 1
        Next column
        gridColumn = 0
        gridRow += 1
    Next Row
Jay
  • 5,897
  • 1
  • 25
  • 28
  • Thank you for your reply. It helped me while finding the item in the grid. I still have one problem, when I search for the item using the above code, it just highlight the last matching item. I want to highlight when 1st matching item is found and when user click on next, the next matching item should be highlighted? Can you help me in this problem? – nightfire001 Nov 02 '11 at 14:23
2

Well you could set the cell's background color with a different color to highlight all the matches, and select only the cell corresponding to the current match:

Dim searchIndex = 0
AccountsDataGridView.ClearSelection()
For Each row As DataGridViewRow In AccountsDataGridView.Rows
    For each cell As DataGridViewCell in row.Cells
        If CStr(cell.Value).Contains(SearchTextBox.Text, StringComparison.OrdinalIgnoreCase) Then
            If searchIndex = m_CurrentSearchIndex Then
                'This is the cell we want to select
                cell.Selected = True
            End If
            'Yellow background for all matches
            cell.Style.BackColor = Color.Yellow
            searchIndex += 1
        End If
    Next
Next

If m_CurrentSearchIndex has a value of 0, it would select the first match, second match for a value of 1, etc.

Meta-Knight
  • 17,626
  • 1
  • 48
  • 58
0
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

 For Each dr As DataGridViewRow In Me.DataGridView1.Rows

            If dr.Cells(0).Value.ToString.Contains(TextBox2.Text) Then dr.Visible = True Else dr.Visible = False


        Next
    End Sub
alaa
  • 1