troubleshooting Question

vb.net datagrid scroll automatically to the first found row

Avatar of PeterBaileyUk
PeterBaileyUk asked on
Visual Basic.NET* DataGridView
11 Comments2 Solutions439 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)
                tb.Clear()
                e.Handled = True
            End If
        End If

    End Sub

extensions
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))
            Else
                table.Columns.Add([property].Name, [property].PropertyType)
            End If
        Next

        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)
            Next
            table.Rows.Add(values)
        Next

        Return table
    End Function

    <Extension()>
    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
        grid.BeginEdit(True)
        Dim counter = 0
        If clearSelected Then
            For Each row In grid.SelectedRows.Cast(Of DataGridViewRow)()
                row.Selected = False
            Next
        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
            Else
                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
                Next
            End If
        Next
        grid.EndEdit()

        If counter = 0 Then MessageBox.Show(String.Format("No rows were found that matched - {0}", term))
    End Sub
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 11 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 11 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros