Click here to Skip to main content
15,891,933 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i created a code where an item is searched in the datagridview and when a user hits enter or down key the focus is set to the datagridview , I want the scroll start from the selected row but it is starting from the last selected row

VB
 Private Function FindItems(ByVal strSearchString As String) As Boolean
    dgvsearchitem.ClearSelection()
 For Each myRow As DataGridViewRow In dgvsearchitem.Rows
        For Each myCell As DataGridViewCell In myRow.Cells
            If InStr(myCell.Value.ToString, strSearchString,CompareMethod.Text) Then
                myRow.Selected = True
                Return True
            End If
        Next
    Next
    Return False

End Function
 Private Sub txtitemsearch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtitemsearch.KeyDown
    If (e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Down Or e.KeyCode = Keys.Tab) And txtiname.Text <> "" Then
        dgvsearchitem.Focus()

    End If

End Sub
Posted
Comments
Ralf Meier 25-Jun-15 4:04am    
Have you tried to select the row AFTER giving it the Focus ?
yash00121 25-Jun-15 6:41am    
i added
dgvsearchitem.FirstDisplayedScrollingRowIndex = dgvsearchitem.SelectedRows(0).Index
but not working .

This code works for me..
I have an own implementation of DataGridView and my data is bound to a BindingSource _BS..
Maybe you can give it a shot.



VB
Public Function FindValue(ByVal tcValue As String, Optional ByVal tcColumn As String = "") As Boolean
   If Not String.IsNullOrEmpty(tcValue.Trim) Then
      Dim lcColumn As String = Me.CurrentCell.OwningColumn.DataPropertyName
      If Not String.IsNullOrEmpty(tcColumn) Then
         lcColumn = tcColumn
      End If
      If String.IsNullOrEmpty(lcColumn) Then
         lcColumn = Me.CurrentCell.OwningColumn.Name 
      End If
      For Each loRow As DataGridViewRow In Me.Rows
         If loRow.Cells(lcColumn).EditedFormattedValue.ToString.ToUpper().StartsWith(tcValue.ToUpper()) Then
            Me._BS.Position = loRow.Index
            Return True
         End If
      Next
   End If
   Return False
End Function
 
Share this answer
 
If your problem is still not solved :

The right Syntax for that use is :
VB
dgvsearchitem.rows(yourIndex).Selected = True
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900