You are on page 1of 4

CONSULTA EN UN DATAGRIDVIEW

Class1.vb Imports System.Data Imports System.Data.SqlClient Public Class Class1 Private con As SqlConnection Private dtb As DataTable Private da As SqlDataAdapter Private lispro As New List(Of Producto) Private Shared dato As String Sub New() con = New SqlConnection _ ("Integrated security=true;server=.;database=ventas") dtb = New DataTable da = New SqlDataAdapter("select * from productos1", con) da.Fill(dtb) For Each f As DataRow In dtb.Rows

lispro.Add(New Producto(f(0), f(1), f(2), f(3))) Next End Sub Public Function Listado() As List(Of Producto) dtb = New DataTable da = New SqlDataAdapter("select * from productos1", con) da.Fill(dtb) For Each f As DataRow In dtb.Rows lispro.Add(New Producto(f(0), f(1), f(2), f(3))) Next Return lispro End Function Public Function Buscar(ByVal pro As String) As Integer dato = pro Dim i As Integer = 0 Try i = lispro.FindIndex(AddressOf compara) Catch ex As Exception Throw New Exception(ex.ToString) End Try Return i End Function Private Shared Function compara(ByVal p As Producto) As Boolean Return p.Producto.ToString().ToUpper().StartsWith(dato.ToUpper) End Function End Class Form1.vb Imports Componente Public Class Form1 Dim obj As Class1 Dim i As Integer = 0 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load obj = New Class1 Me.DataGridView1.DataSource = obj.Listado() End Sub Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged Try Me.DataGridView1.Rows(i).Selected = False i = obj.Buscar(Me.TextBox1.Text)

Me.DataGridView1.Rows(i).Selected = True Me.DataGridView1.FirstDisplayedScrollingRowIndex = i Catch ex As Exception MsgBox(ex.Message) End Try End Sub End Class Producto.vb Public Class Producto Private _codigo As Integer Private _producto As String Private _precio As Double Private _stock As Integer Sub New() End Sub Sub New(ByVal codigo As Integer, ByVal producto As String, ByVal precio As Double, ByVal stock As Integer) _codigo = codigo _producto = producto _precio = precio _stock = stock End Sub Public Property Codigo() Get Return _codigo End Get Set(ByVal value) _codigo = value End Set End Property Public Property Producto() Get Return _producto End Get Set(ByVal value) _producto = value End Set End Property Public Property Precio() Get Return _precio End Get Set(ByVal value) _precio = value End Set

End Property Public Property Stock() Get Return _stock End Get Set(ByVal value) _stock = value End Set End Property End Class

You might also like