none
how to search in datagridview and show result when NEXT and Color Rows ?

    Question

  • hi , when search for text in datagridview I want show result when click Button NEXT and Color Rows and move pointer between results ???

    Help code ....

    my code :

    Public Class Form1
        Private searchIndex As Integer = -1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ListView1.View = View.Details
            ListView1.Columns.Add("Column")
            ListView1.Columns.Add("Row")
            ListView1.Columns.Add("Value")
            '-----------------------------------------------
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Button1.Text = "Find All"
            Dim tempIndex As Integer = -1
            ListView1.Items.Clear()
    
            For i As Integer = 0 To DataGridView1.Rows.Count - 1
                tempIndex = (tempIndex + 1) Mod DataGridView1.Rows.Count
                Dim row As DataGridViewRow = DataGridView1.Rows(tempIndex)
    
                If row.Cells("Foo").Value Is Nothing Then
                    Continue For
                End If
    
                If row.Cells("Foo").Value.ToString().Trim().StartsWith(TextBox1.Text) OrElse row.Cells("Foo").Value.ToString().Trim().Contains(TextBox1.Text) Then
                    Dim cell As DataGridViewCell = row.Cells("Foo")
                    Dim lvRow As New ListViewItem(New String() {cell.ColumnIndex.ToString(), cell.RowIndex.ToString(), cell.Value.ToString()})
                    ListView1.Items.Add(lvRow)
                End If
            Next
    
            MessageBox.Show("Search ended.")
            ' Or whatever message you intend to send.
        End Sub
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            searchIndex = -1
        End Sub
    End Class





    • Edited by srajmuneer Saturday, April 8, 2017 10:08 PM
    Saturday, April 8, 2017 9:46 PM

Answers

  • Hi

    Have a go with this modified version

    ' This example needs a Form1 with
    ' a blank DataGridView1, TextBox1
    ' Button1 and ListBox1
    Option Strict On
    Option Explicit On
    Option Infer Off
    Public Class Form1
        Dim myTable As New DataTable("Freddy")
        Dim view As New DataView(myTable)
        Dim Prs As New List(Of Pr)
        Dim Hcol As Color = Color.DarkRed
        Dim Hfnt As Font = New Font("Arial", 12, FontStyle.Bold)
        Dim defCol As Color = Color.Black
        Dim defFnt As Font = Nothing
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'add some dummy data
            With myTable
                .Columns.Add("Name", GetType(String))
                .Columns.Add("Roll Number", GetType(String))
                .Columns.Add("Subject1", GetType(String))
                .Columns.Add("Subject2", GetType(String))
                .Columns.Add("Score", GetType(String))
                .Rows.Add("N1", 5, "Math", "Phy", 20.7456D)
                .Rows.Add("N2", 3, "Social", "Chem", 15.5D)
                .Rows.Add("N1", 1, "Math", "Phy", 11.5D)
                .Rows.Add("N2", 3, "Social", "Math", 25.7D)
                .Rows.Add("N1", 2, "Math", "Phy", 20.9D)
                .Rows.Add("N2", 1, "Social", "Math", 18.5D)
                .Rows.Add("N1", 2, "English", "Phy", 23.5D)
                .Rows.Add("N2", 3, "Social", "Math", 25.7D)
            End With
            ' adjust DGV properties
            With DataGridView1
                .DataSource = view
                .MultiSelect = False
                .RowHeadersWidth = 24
                .ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.True
                .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
                .SelectionMode = DataGridViewSelectionMode.CellSelect
                For i As Integer = 0 To 4
                    .Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                Next
            End With
            ' grab initial font
            defFnt = DataGridView1.DefaultCellStyle.Font
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If Trim(TextBox1.Text).Length < 1 Then Exit Sub
            Srch(Trim(TextBox1.Text))
        End Sub
        Sub Srch(s As String)
            ListBox1.Items.Clear()
            Prs.Clear()
            For Each r As DataGridViewRow In DataGridView1.Rows
                If Not r.Index = DataGridView1.NewRowIndex Then
                    For Each c As DataGridViewCell In r.Cells
                        If c.Value.ToString.ToLower.Contains(s.ToLower) Then
                            c.Style.ForeColor = Hcol
                            c.Style.Font = Hfnt
                            ListBox1.Items.Add("Row " & c.RowIndex.ToString & " Col " & c.ColumnIndex.ToString & " Title " & c.OwningColumn.HeaderText)
                            Prs.Add(New Pr With {.Row = c.RowIndex, .Column = c.ColumnIndex})
                        Else
                            c.Style.ForeColor = defCol
                            c.Style.Font = defFnt
                        End If
                    Next
                End If
            Next
        End Sub
        Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
            Dim lb As ListBox = DirectCast(sender, ListBox)
            DataGridView1.ClearSelection()
            DataGridView1(Prs(lb.SelectedIndex).Column, Prs(lb.SelectedIndex).Row).Selected = True
        End Sub
        Class Pr
            Property Row As Integer
            Property Column As Integer
        End Class
    End Class
    
    


    Regards Les, Livingston, Scotland

    • Marked as answer by srajmuneer Thursday, April 13, 2017 8:33 PM
    Monday, April 10, 2017 1:06 PM
  • Hi

    Here is another version with search as you type into the textbox instead of using a Button Click.

    ' This example needs a Form1 with
    ' a blank DataGridView1, TextBox1
    ' and ListBox1
    Option Strict On
    Option Explicit On
    Option Infer Off
    Public Class Form1
        Dim myTable As New DataTable("Freddy")
        Dim view As New DataView(myTable)
        Dim Prs As New List(Of Pr)
        Dim Hcol As Color = Color.DarkRed
        Dim Hfnt As Font = New Font("Arial", 12, FontStyle.Bold)
        Dim defCol As Color = Color.Black
        Dim defFnt As Font = Nothing
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'add some dummy data
            With myTable
                .Columns.Add("Name", GetType(String))
                .Columns.Add("Roll Number", GetType(String))
                .Columns.Add("Subject1", GetType(String))
                .Columns.Add("Subject2", GetType(String))
                .Columns.Add("Score", GetType(String))
                .Rows.Add("N1", 5, "Math", "Phy", 20.7456D)
                .Rows.Add("N2", 3, "Social", "Chem", 15.5D)
                .Rows.Add("N1", 1, "Math", "Phy", 11.5D)
                .Rows.Add("N2", 3, "Social", "Math", 25.7D)
                .Rows.Add("N1", 2, "Math", "Phy", 20.9D)
                .Rows.Add("N2", 1, "Social", "Math", 18.5D)
                .Rows.Add("N1", 2, "English", "Phy", 23.5D)
                .Rows.Add("N2", 3, "Social", "Math", 25.7D)
            End With
            ' adjust DGV properties
            With DataGridView1
                .DataSource = view
                .MultiSelect = False
                .RowHeadersWidth = 24
                .ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.True
                .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
                .SelectionMode = DataGridViewSelectionMode.CellSelect
                For i As Integer = 0 To 4
                    .Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                Next
            End With
            ' grab initial font
            defFnt = DataGridView1.DefaultCellStyle.Font
        End Sub
        Sub Srch(s As String)
            ListBox1.Items.Clear()
            Prs.Clear()
            DataGridView1.ClearSelection()
            For Each r As DataGridViewRow In DataGridView1.Rows
                If Not r.Index = DataGridView1.NewRowIndex Then
                    For Each c As DataGridViewCell In r.Cells
                        If s.Length > 0 AndAlso c.Value.ToString.ToLower.Contains(s.ToLower) Then
                            c.Style.ForeColor = Hcol
                            c.Style.Font = Hfnt
                            ListBox1.Items.Add("Row " & c.RowIndex.ToString & " Col " & c.ColumnIndex.ToString & " Title " & c.OwningColumn.HeaderText)
                            Prs.Add(New Pr With {.Row = c.RowIndex, .Column = c.ColumnIndex})
                        Else
                            c.Style.ForeColor = defCol
                            c.Style.Font = defFnt
                        End If
                    Next
                End If
            Next
        End Sub
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            Srch(Trim(TextBox1.Text))
        End Sub
        Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
            Dim lb As ListBox = DirectCast(sender, ListBox)
            DataGridView1.ClearSelection()
            DataGridView1(Prs(ListBox1.SelectedIndex).Column, Prs(ListBox1.SelectedIndex).Row).Selected = True
            TextBox1.Select()
        End Sub
        Class Pr
            Property Row As Integer
            Property Column As Integer
        End Class
    End Class
    
    



    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, April 10, 2017 5:45 PM
    • Marked as answer by srajmuneer Thursday, April 13, 2017 8:33 PM
    Monday, April 10, 2017 5:32 PM

All replies

  • Hi

    Yet another set of questions insteadof answers!

    What does the DataGridView contain?

    What is the expected 'Result' you mention?

    What is 'NEXT' -a button perhaps?

    You talk about 'color rows', but you don't show anything to do with that in your code?

    You want to move 'pointer' between results - what is 'pointer' and where is it located?  Is it a Cell in a DataGridView, a row in a Datagridview, text in a ListView?

    So many questions,so few answers!


    Regards Les, Livingston, Scotland


    • Edited by leshay Saturday, April 8, 2017 10:52 PM
    Saturday, April 8, 2017 10:50 PM
  • Conceptually speaking the following pattern should do the trick unless we are talking a lot of rows. You should consider loading the DataGridView via it's DataSource and bind to a BindingSource so not to trigger DataGridView events.

    So here is the conceptual example against the DataGridView and not it's DataSource excluding coloring cells or rows.

    Public Class Form1
        Private rowIndexList As New List(Of Integer)
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim found As Boolean = False
            Dim results = DataGridView1.Rows.Cast(Of DataGridViewRow) _
                .Select(Function(row, index) New SearchItem With {.row = row, .RowIndex = index}) _
                .Where(Function(item)
                           Return item.row.IsNewRow = False AndAlso
                           CStr(item.row.Cells("Column1").Value).ToLower = TextBox1.Text.ToLower
                       End Function).ToList
    
            For Each item As SearchItem In results
                If Not rowIndexList.Contains(item.RowIndex) Then
                    DataGridView1.CurrentCell = DataGridView1(0, item.RowIndex)
                    rowIndexList.Add(item.RowIndex)
                    found = True
                    Exit For
                End If
            Next
    
            If Not found AndAlso rowIndexList.Count > 0 Then
                MessageBox.Show("All instances of search string have been located")
            ElseIf Not found AndAlso rowIndexList.Count = 0 Then
                MessageBox.Show("Failed to located search string")
            End If
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.Rows.Add(New Object() {"Ann"})
            DataGridView1.Rows.Add(New Object() {"Karen"})
            DataGridView1.Rows.Add(New Object() {"Karen"})
            DataGridView1.Rows.Add(New Object() {"Joan"})
            DataGridView1.Rows.Add(New Object() {"Karen"})
            DataGridView1.Rows.Add(New Object() {"Jill"})
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            rowIndexList.Clear()
        End Sub
    End Class
    
    Public Class SearchItem
        Public Property row As DataGridViewRow
        Public Property RowIndex As Integer
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, April 8, 2017 11:53 PM
    Moderator
  • thanks .. I try code but show error in the :

    Sunday, April 9, 2017 11:15 AM
  • You need to use Framework 3.5 or higher for Cast(Of DataGridViewRow)

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, April 9, 2017 11:27 AM
    Moderator
  • thank you all >>>
    1.my datagridview contain is data .
    2.I want search for text in all rows and all columns .
    3.show result into listbox or listview .
    4.I want Find Next like excel .
    5.change color text (result text) in rows in datagridview .
    Sunday, April 9, 2017 5:09 PM
  • thank you all >>>
    1.my datagridview contain is data .
    2.I want search for text in all rows and all columns .
    3.show result into listbox or listview .
    4.I want Find Next like excel .
    5.change color text (result text) in rows in datagridview .

    Hi

    Here is some code that may offer some pointers for you. As I don't have Excel, I have just guessed as to the contents of the ListBox1

    ' This example needs a Form1 with
    ' a blank DataGridView1, TextBox1
    ' Button1 and ListBox1
    Option Strict On
    Option Explicit On
    Option Infer Off
    Public Class Form1
        Dim myTable As New DataTable("Freddy")
        Dim view As New DataView(myTable)
        Dim Hcol As Color = Color.DarkRed
        Dim Hfnt As Font = New Font("Arial", 12, FontStyle.Bold)
        Dim defCol As Color = Color.Black
        Dim defFnt As Font = Nothing
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'add some dummy data
            With myTable
                .Columns.Add("Name", GetType(String))
                .Columns.Add("Roll Number", GetType(String))
                .Columns.Add("Subject1", GetType(String))
                .Columns.Add("Subject2", GetType(String))
                .Columns.Add("Score", GetType(String))
                .Rows.Add("N1", 5, "Math", "Phy", 20.7456D)
                .Rows.Add("N2", 3, "Social", "Chem", 15.5D)
                .Rows.Add("N1", 1, "Math", "Phy", 11.5D)
                .Rows.Add("N2", 3, "Social", "Math", 25.7D)
                .Rows.Add("N1", 2, "Math", "Phy", 20.9D)
                .Rows.Add("N2", 1, "Social", "Math", 18.5D)
                .Rows.Add("N1", 2, "English", "Phy", 23.5D)
                .Rows.Add("N2", 3, "Social", "Math", 25.7D)
            End With
            ' adjust DGV properties
            With DataGridView1
                .DataSource = myTable
                .MultiSelect = False
                .RowHeadersWidth = 24
                .ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.True
                .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
                .SelectionMode = DataGridViewSelectionMode.CellSelect
                For i As Integer = 0 To 4
                    .Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                Next
            End With
            DataGridView1.DataSource = view
            ' grab initial font
            defFnt = DataGridView1.DefaultCellStyle.Font
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If Trim(TextBox1.Text).Length < 1 Then Exit Sub
            Srch(Trim(TextBox1.Text))
        End Sub
        Sub Srch(s As String)
            ListBox1.Items.Clear()
            For Each r As DataGridViewRow In DataGridView1.Rows
                If Not r.Index = DataGridView1.NewRowIndex Then
                    For Each c As DataGridViewCell In r.Cells
                        If c.Value.ToString.ToLower = s.ToLower Then
                            c.Style.ForeColor = Hcol
                            c.Style.Font = Hfnt
                            ListBox1.Items.Add("Row " & c.RowIndex.ToString & " Col " & c.ColumnIndex.ToString & " Title " & c.OwningColumn.HeaderText)
                        Else
                            c.Style.ForeColor = defCol
                            c.Style.Font = defFnt
                        End If
                    Next
                End If
            Next
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    Sunday, April 9, 2017 5:46 PM
  • thank you leshay , this code very good , I have some simple additions :

    1. I want search for any part of text in all rows and all columns .
    2. when click in result into listbox1 The indicator goes to required row .
    Monday, April 10, 2017 6:07 AM
  • Hi

    Have a go with this modified version

    ' This example needs a Form1 with
    ' a blank DataGridView1, TextBox1
    ' Button1 and ListBox1
    Option Strict On
    Option Explicit On
    Option Infer Off
    Public Class Form1
        Dim myTable As New DataTable("Freddy")
        Dim view As New DataView(myTable)
        Dim Prs As New List(Of Pr)
        Dim Hcol As Color = Color.DarkRed
        Dim Hfnt As Font = New Font("Arial", 12, FontStyle.Bold)
        Dim defCol As Color = Color.Black
        Dim defFnt As Font = Nothing
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'add some dummy data
            With myTable
                .Columns.Add("Name", GetType(String))
                .Columns.Add("Roll Number", GetType(String))
                .Columns.Add("Subject1", GetType(String))
                .Columns.Add("Subject2", GetType(String))
                .Columns.Add("Score", GetType(String))
                .Rows.Add("N1", 5, "Math", "Phy", 20.7456D)
                .Rows.Add("N2", 3, "Social", "Chem", 15.5D)
                .Rows.Add("N1", 1, "Math", "Phy", 11.5D)
                .Rows.Add("N2", 3, "Social", "Math", 25.7D)
                .Rows.Add("N1", 2, "Math", "Phy", 20.9D)
                .Rows.Add("N2", 1, "Social", "Math", 18.5D)
                .Rows.Add("N1", 2, "English", "Phy", 23.5D)
                .Rows.Add("N2", 3, "Social", "Math", 25.7D)
            End With
            ' adjust DGV properties
            With DataGridView1
                .DataSource = view
                .MultiSelect = False
                .RowHeadersWidth = 24
                .ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.True
                .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
                .SelectionMode = DataGridViewSelectionMode.CellSelect
                For i As Integer = 0 To 4
                    .Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                Next
            End With
            ' grab initial font
            defFnt = DataGridView1.DefaultCellStyle.Font
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If Trim(TextBox1.Text).Length < 1 Then Exit Sub
            Srch(Trim(TextBox1.Text))
        End Sub
        Sub Srch(s As String)
            ListBox1.Items.Clear()
            Prs.Clear()
            For Each r As DataGridViewRow In DataGridView1.Rows
                If Not r.Index = DataGridView1.NewRowIndex Then
                    For Each c As DataGridViewCell In r.Cells
                        If c.Value.ToString.ToLower.Contains(s.ToLower) Then
                            c.Style.ForeColor = Hcol
                            c.Style.Font = Hfnt
                            ListBox1.Items.Add("Row " & c.RowIndex.ToString & " Col " & c.ColumnIndex.ToString & " Title " & c.OwningColumn.HeaderText)
                            Prs.Add(New Pr With {.Row = c.RowIndex, .Column = c.ColumnIndex})
                        Else
                            c.Style.ForeColor = defCol
                            c.Style.Font = defFnt
                        End If
                    Next
                End If
            Next
        End Sub
        Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
            Dim lb As ListBox = DirectCast(sender, ListBox)
            DataGridView1.ClearSelection()
            DataGridView1(Prs(lb.SelectedIndex).Column, Prs(lb.SelectedIndex).Row).Selected = True
        End Sub
        Class Pr
            Property Row As Integer
            Property Column As Integer
        End Class
    End Class
    
    


    Regards Les, Livingston, Scotland

    • Marked as answer by srajmuneer Thursday, April 13, 2017 8:33 PM
    Monday, April 10, 2017 1:06 PM
  • Hi

    Here is another version with search as you type into the textbox instead of using a Button Click.

    ' This example needs a Form1 with
    ' a blank DataGridView1, TextBox1
    ' and ListBox1
    Option Strict On
    Option Explicit On
    Option Infer Off
    Public Class Form1
        Dim myTable As New DataTable("Freddy")
        Dim view As New DataView(myTable)
        Dim Prs As New List(Of Pr)
        Dim Hcol As Color = Color.DarkRed
        Dim Hfnt As Font = New Font("Arial", 12, FontStyle.Bold)
        Dim defCol As Color = Color.Black
        Dim defFnt As Font = Nothing
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'add some dummy data
            With myTable
                .Columns.Add("Name", GetType(String))
                .Columns.Add("Roll Number", GetType(String))
                .Columns.Add("Subject1", GetType(String))
                .Columns.Add("Subject2", GetType(String))
                .Columns.Add("Score", GetType(String))
                .Rows.Add("N1", 5, "Math", "Phy", 20.7456D)
                .Rows.Add("N2", 3, "Social", "Chem", 15.5D)
                .Rows.Add("N1", 1, "Math", "Phy", 11.5D)
                .Rows.Add("N2", 3, "Social", "Math", 25.7D)
                .Rows.Add("N1", 2, "Math", "Phy", 20.9D)
                .Rows.Add("N2", 1, "Social", "Math", 18.5D)
                .Rows.Add("N1", 2, "English", "Phy", 23.5D)
                .Rows.Add("N2", 3, "Social", "Math", 25.7D)
            End With
            ' adjust DGV properties
            With DataGridView1
                .DataSource = view
                .MultiSelect = False
                .RowHeadersWidth = 24
                .ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.True
                .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
                .SelectionMode = DataGridViewSelectionMode.CellSelect
                For i As Integer = 0 To 4
                    .Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                Next
            End With
            ' grab initial font
            defFnt = DataGridView1.DefaultCellStyle.Font
        End Sub
        Sub Srch(s As String)
            ListBox1.Items.Clear()
            Prs.Clear()
            DataGridView1.ClearSelection()
            For Each r As DataGridViewRow In DataGridView1.Rows
                If Not r.Index = DataGridView1.NewRowIndex Then
                    For Each c As DataGridViewCell In r.Cells
                        If s.Length > 0 AndAlso c.Value.ToString.ToLower.Contains(s.ToLower) Then
                            c.Style.ForeColor = Hcol
                            c.Style.Font = Hfnt
                            ListBox1.Items.Add("Row " & c.RowIndex.ToString & " Col " & c.ColumnIndex.ToString & " Title " & c.OwningColumn.HeaderText)
                            Prs.Add(New Pr With {.Row = c.RowIndex, .Column = c.ColumnIndex})
                        Else
                            c.Style.ForeColor = defCol
                            c.Style.Font = defFnt
                        End If
                    Next
                End If
            Next
        End Sub
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            Srch(Trim(TextBox1.Text))
        End Sub
        Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
            Dim lb As ListBox = DirectCast(sender, ListBox)
            DataGridView1.ClearSelection()
            DataGridView1(Prs(ListBox1.SelectedIndex).Column, Prs(ListBox1.SelectedIndex).Row).Selected = True
            TextBox1.Select()
        End Sub
        Class Pr
            Property Row As Integer
            Property Column As Integer
        End Class
    End Class
    
    



    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, April 10, 2017 5:45 PM
    • Marked as answer by srajmuneer Thursday, April 13, 2017 8:33 PM
    Monday, April 10, 2017 5:32 PM