DataGridView Search

BlackByte

Well-known member
Joined
Jun 29, 2008
Messages
126
Location
South Africa, Durban
Programming Experience
1-3
Hi i am using the following code to search my datagridview
VB.NET:
 Try

            Dim i As Integer
            Dim found As Boolean = False
            Dim empCode As String = txtSearch.Text
            'Make the currently selected rows lose focus to prevent two rows being highlighted
            For i = DGV.SelectedRows.Count - 1 To 0 Step -1
                DGV.SelectedRows(i).Selected = False
            Next
            For i = 0 To DGV.Rows.Count - 1
                If String.Compare(empCode, DGV.Rows(i).Cells("Employee_Code").Value) = False Then 'If false it means they are the same
                    MsgBox(DGV.Rows(i).Cells("Employee_Code").Value & " found at row " & DGV.Rows(i).Index + 1)
                    DGV.Rows(i).Selected = True 'Highlight searched row
                    found = True
                    Exit For
                End If
            Next

            If found = False Then
                MessageBox.Show("An employee with the code " & "'" & empCode & "'" & " was not found.", "'" & empCode & "'" & " not found.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If

        Catch ex As Exception

        End Try
[code]
The code works fine and the correct row is selected. The problem is, if there are many items in the datagridview, i have to scroll down to see which item is selected, i was wondering if there is a way to udjust the scroll bars so that the found row is selected and shown to the user.

Let me know if this doesn't make sense so I can explain futher..
 
Back
Top