segunda-feira, 3 de setembro de 2012

Search and Select in Datagridview





Public Function FindinDGV(ByVal DGV As DataGridView, ByVal TXT As TextBox, ByVal STR As String) As Boolean
'or instead of STR as string use anoter type like INTG as integer...

DGV.ClearSelection() 'Clear selected row before searching
Dim intcount As Integer = 0

For Each Row As DataGridViewRow In DGV.Rows' Search in all rows

  If Trim(DGV.Rows(intcount).Cells(STR).Value).ToString.ToUpper =    .     TXT.Text.ToUpper Then

     DGV.Rows(intcount).Selected = True    
     DGV.FirstDisplayedScrollingRowIndex = intcount
     DGV.Rows(intcount).Cells(STR).Selected =
True 'Select the find it rows   
     FindinDGV = True    
     TXT.Clear()  
     Exit Function 'If has found then exit from the function 

 End If 

 intcount += 1Next Row
 FindinDGV = False

 'If false then has no result in the search
 MsgBox("There is no " & TXT.Text & " !")

 TXT.Clear()  
End Function

'How to use the function
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

  Dim yourSTRING As String = "....Ex: Luis... "
  FindinDGV(yourDATAGRIDVIEW, yourTEXTBOXusedToSearch, yourSTRING)

End Sub





Sem comentários:

Enviar um comentário