Foros del Web » Programación para mayores de 30 ;) » .NET »

Ayuda al buscar un dato repetido en un Datagridview

Estas en el tema de Ayuda al buscar un dato repetido en un Datagridview en el foro de .NET en Foros del Web. Hola que tal, tengo la siguiente consulta: Necesito buscar un dato repetivo en la grilla al darle enter, este es el codigo que tengo hasta ...
  #1 (permalink)  
Antiguo 07/01/2010, 09:26
 
Fecha de Ingreso: diciembre-2007
Mensajes: 5
Antigüedad: 16 años, 4 meses
Puntos: 0
Ayuda al buscar un dato repetido en un Datagridview

Hola que tal, tengo la siguiente consulta:

Necesito buscar un dato repetivo en la grilla al darle enter, este es el codigo que tengo hasta ahora

Código:
'Contador
Private vldContador As Double = 0

Private Sub oTx_BuscaNombre_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles oTx_BuscaNombre.KeyPress
        Dim i As Double

        If e.KeyChar = ChrW(Keys.Enter) Then
            For i = vldContador To oGvDatosClase.RowCount - 1
                If oGvDatosClase.Rows(i).Cells("NOMBRE").Value = UCase(oTx_BuscaNombre.Text) Then
                    oGvDatosClase.Rows(i).Selected = True
                    oGvDatosClase.Rows(i).Selected = i
                    vldContador = i + 1
                    Exit For
                End If

        'Para buscar otro dato
        If e.KeyChar = ChrW(Keys.Back) Or e.KeyChar = ChrW(Keys.Delete) Then
            vldContador = 0
        End If
    End Sub
ya lo que me busca ahi es la coincidencia con la caja de texto

si yo tuviera en mi grilla los siguientes datos (ej)

Cod Nombre
1 Alambre
2 Cobre
3 Alfombra
4 Tubo
5 Alicate
6 Alambre
7 Ampolleta


si yo busco Alambre me salta en el 1 y el 6

Ahora lo que yo necesito es si al escribir Al, quiero que salte al 1,3,5,6

Alguna idea gracias
  #2 (permalink)  
Antiguo 07/01/2010, 13:21
Avatar de eperedo  
Fecha de Ingreso: septiembre-2009
Ubicación: Perú
Mensajes: 654
Antigüedad: 14 años, 6 meses
Puntos: 16
Respuesta: Ayuda al buscar un dato repetido en un Datagridview

Podría resultarte usando Contains en la comparación, algo así:
Cita:
Iniciado por Sonsuke Ver Mensaje
Código:
'Contador
Private vldContador As Double = 0

Private Sub oTx_BuscaNombre_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles oTx_BuscaNombre.KeyPress
        Dim i As Double

        If e.KeyChar = ChrW(Keys.Enter) Then
            For i = vldContador To oGvDatosClase.RowCount - 1
                If Convert.ToString(oGvDatosClase.Rows(i).Cells("NOMBRE").Value).Contains(UCase(oTx_BuscaNombre.Text)) Then
                    oGvDatosClase.Rows(i).Selected = True
                    oGvDatosClase.Rows(i).Selected = i
                    vldContador = i + 1
                    Exit For
                End If

        'Para buscar otro dato
        If e.KeyChar = ChrW(Keys.Back) Or e.KeyChar = ChrW(Keys.Delete) Then
            vldContador = 0
        End If
    End Sub
Espero te sirva
__________________
Eduardo Peredo
Wigoin
  #3 (permalink)  
Antiguo 07/01/2010, 14:26
 
Fecha de Ingreso: diciembre-2007
Mensajes: 5
Antigüedad: 16 años, 4 meses
Puntos: 0
Respuesta: Ayuda al buscar un dato repetido en un Datagridview

Gracias por la respuesta comentar tambien que funciona de esta otra forma igual

Código:
'Contador
Private vldContador As Double = 0

Private Sub oTx_BuscaNombre_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles oTx_BuscaNombre.KeyPress
        Dim i As Double
        Dim posicion As Double
        Dim vlsNombre As String
        Dim vlsComparar As String


        If e.KeyChar = ChrW(Keys.Enter) Then
            vlsComparar = UCase(oTx_BuscaNombre.Text)
            For i = vldContador To oGvDatosClase.RowCount - 1
                vlsNombre = oGvDatosClase.Rows(i).Cells("NOMBRE").Value
                posicion = InStr(vlsNombre, vlsComparar)
                If posicion <> 0 Then
                    oGvDatosClase.Rows(i).Selected = True
                    oGvDatosClase.Rows(i).Selected = i
                    vldContador = i + 1
                    Exit For
                End If
            Next
        End If

        If e.KeyChar = ChrW(Keys.Back) Or e.KeyChar = ChrW(Keys.Delete) Then
            vldContador = 0
        End If
    End Sub
lo que me ocurre ahora es al tener

Cod Nombre
1 Alambre
2 Cobre
3 Alfombra
4 Tubo
5 Alicate
6 Alambre
7 Ampolleta
...

100 Albañileria

al poner "Al" salta por todos los elementos pero en el 100 en el datagridview lo hace pero el scroll no baja a esa posicion



Edito: agregando esta propiedad se logra FirstDisplayedScrollingRowIndex


Código:
'Contador
Private vldContador As Double = 0

Private Sub oTx_BuscaNombre_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles oTx_BuscaNombre.KeyPress
        Dim i As Double

        'Si ocupamos la 2º forma
        '-------------------------------------------
        Dim posicion As Double        
        Dim vlsNombre As String
        Dim vlsComparar As String
        '--------------------------------------------


        If e.KeyChar = ChrW(Keys.Enter) Then
            
            'Si ocupamos la 2º forma
            vlsComparar = UCase(oTx_BuscaNombre.Text)

            For i = vldContador To oGvDatosClase.RowCount - 1
                '1º Forma
                 If Convert.ToString(oGvDatosClase.Rows(i).Cells("NOMBRE").Value).Contains(UCase(oTx_BuscaNombre.Text)) Then
                    oGvDatosClase.Rows(i).Selected = True
                    oGvDatosClase.Rows(i).Selected = i
                    oGvDatosClase.FirstDisplayedScrollingRowIndex = i
                    vldContador = i + 1
                    Exit For
                End If

                 'o pueden ocupar esta 2º Forma de igual manera
                vlsNombre = oGvDatosClase.Rows(i).Cells("NOMBRE").Value
                posicion = InStr(vlsNombre, vlsComparar)
                If posicion <> 0 Then
                    oGvDatosClase.Rows(i).Selected = True
                    oGvDatosClase.Rows(i).Selected = i
                    oGvDatosClase.FirstDisplayedScrollingRowIndex = i
                    vldContador = i + 1
                    Exit For
                End If
            Next
        End If

        If e.KeyChar = ChrW(Keys.Back) Or e.KeyChar = ChrW(Keys.Delete) Then
            vldContador = 0
        End If
    End Sub
ahi ordene el codigo y que ojala le sirva a alguien mas, agregue la solucion de eperedo igual

Saludos

Última edición por Sonsuke; 07/01/2010 a las 15:02

Etiquetas: datagridview, dato, repetido
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 04:58.