Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

vb.net datagrid scroll automatically to the first found row

Avatar of PeterBaileyUk
PeterBaileyUk asked on
Visual Basic.NET* DataGridView
11 Comments1 Solution439 ViewsLast Modified:
I have a sub that does a search and selects (highlights) the rows. How can I make it scroll to that first row automatically?

    Private Shadows Sub OnKeyUp(sender As Object, e As KeyEventArgs) Handles TxtBulkShortDesc.KeyUp
        Dim tb = DirectCast(sender, TextBox)
        If tb.Equals(TxtBulkShortDesc) Then
            If e.KeyCode.Equals(Keys.Enter) Then
                DataGridViewStringsBulk.SearchAndSelect(tb.Text, CBBulkSearchDesc.SelectedItem, True)
                e.Handled = True
            End If
        End If

    End Sub

Public Function ConvertToDataTable(Of T)(ByVal source As IEnumerable(Of T)) As DataTable
        Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(GetType(T))
        Dim table As DataTable = New DataTable()

        For i As Integer = 0 To properties.Count - 1
            Dim [property] As PropertyDescriptor = properties(i)
            If [property].PropertyType.IsGenericType AndAlso [property].PropertyType.GetGenericTypeDefinition().Equals(GetType(Nullable)) Then
                table.Columns.Add([property].Name, [property].PropertyType.GetGenericArguments()(0))
                table.Columns.Add([property].Name, [property].PropertyType)
            End If

        Dim values(properties.Count - 1) As Object
        For Each item As T In source
            For i As Integer = 0 To properties.Count - 1
                values(i) = properties(i).GetValue(item)

        Return table
    End Function

    Public Sub SearchAndSelect(grid As DataGridView, term As String, selector As String, Optional clearSelected As Boolean = False)
        If grid IsNot Nothing AndAlso grid.Rows.Count < 1 Then Return
        If String.IsNullOrWhiteSpace(term) Then Return
        Dim counter = 0
        If clearSelected Then
            For Each row In grid.SelectedRows.Cast(Of DataGridViewRow)()
                row.Selected = False
        End If

        For Each row In grid.Rows.Cast(Of DataGridViewRow)()
            If selector <> "- All Columns -" Then
                If row.Cells(selector).Value IsNot Nothing AndAlso row.Cells(selector).Value.ToString().IndexOf(term, StringComparison.OrdinalIgnoreCase) <> -1 Then
                    row.Selected = True
                    counter += 1
                    Continue For
                End If
                For Each column In grid.Columns.Cast(Of DataGridViewColumn)()
                    If row.Cells(column.Name).Value IsNot Nothing AndAlso row.Cells(column.Name).Value.ToString().IndexOf(term, StringComparison.OrdinalIgnoreCase) <> -1 Then
                        row.Selected = True
                        counter += 1
                        Exit For
                    End If
            End If

        If counter = 0 Then MessageBox.Show(String.Format("No rows were found that matched - {0}", term))
    End Sub
Avatar of Fernando Soto
This problem has been solved!
Unlock 1 Answer and 11 Comments.
See Answers