Ver Mensaje Individual
  #2 (permalink)  
Antiguo 16/06/2009, 08:13
Avatar de Dradi7
Dradi7
 
Fecha de Ingreso: junio-2008
Ubicación: Peru - Lima
Mensajes: 1.518
Antigüedad: 15 años, 10 meses
Puntos: 220
Respuesta: Filtrar un DataGridView VB.Net

Esta seria una forma correcto de filtrar en un datagridview debes usar el DataView

Ejm:

Código vb.net:
Ver original
  1. Dim Dv as new Dataview
  2. Dim Con as new SqlConnection
  3. Dim Com as new SqlCommand
  4. Dim Adp as new SqlDataAdapter
  5. Dim Ds as new DataSet
  6. Dim SQL as String 'Cadena para el Filtrado
  7.  
  8. ' En el load de tu formulario
  9.     Private Sub frm_filtroprueba(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  10.              try
  11.                 Con = new SqlConnecion(cadena de conexion)
  12.                 if Con.State = 0 then Con.Open()
  13.                 Com = new SqlCommand
  14.                 With Com
  15.                         .Connection = Con
  16.                         .CommandType = CommandType.Text
  17.                         .CommandText = "Select * From TuTabla"
  18.                 end With
  19.                 Adp = new SqlDataAdapter(Com)
  20.                 Adp.fill(ds,"x")
  21.                 datagridview1.DataSource = ds.tables("x") ' Rellenamos la grilla con la tabla
  22.                 Dv.Table = ds.tables("x") ' Enlazamos el dataview con la tabla devuelta
  23.              catch ex as exception
  24.                     msgbox(ex.message,48)
  25.              end try
  26.     End Sub
  27.  
  28. ' en una caja de texto en el evento TextChanged
  29. ' este evento permite filtrar cada vez que ingreses o quites caracteres en la caja de texto
  30.     Private Sub Txt_Filtro_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Txt_Filtro.TextChanged
  31.               try
  32.                   SQL = "Nombre like '%"+Txt_Filtro.Text.Trim+"%'"
  33.                   Dv.RowFilter = SQL
  34.                   datagridview1.DataSource = Dv
  35.                   datagridview1.Update()
  36.               cath ex as exception
  37.                       msgbox(ex.message,48)
  38.               end try
  39.     End Sub


Nota: el codigo es solo de ejemplo tu lo puedes aplicar en programacion de 3 capas y el filtro lo puedes cambiar por el nombre de columna que quieras a la ves de agregarle varias columnas a filtrar

Ejm:

SQL = "Nombre like '%"+Txt_Filtro.Text.Trim+"%' and Apellido like '%"+Txt_Filtro.Text.Trim+"%'"
o
SQL = "Nombre = '"+Txt_Filtro.Text.Trim+"'"
__________________
La clave de todo triunfador es eliminar todas sus excusas y sus limitaciones