Imports VBNCapasAccesoDatos 'Proyecto de Acceso a Datos Public Class VBCategProductos Inherits System.Windows.Forms.Form #Region " Código generado por el Diseñador de Windows Forms " Public Sub New() MyBase.New() 'El Diseñador de Windows Forms requiere esta llamada. InitializeComponent() 'Agregar cualquier inicialización después de la llamada a InitializeComponent() End Sub 'Form reemplaza a Dispose para limpiar la lista de componentes. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Requerido por el Diseñador de Windows Forms Private components As System.ComponentModel.IContainer 'NOTA: el Diseñador de Windows Forms requiere el siguiente procedimiento 'Puede modificarse utilizando el Diseñador de Windows Forms. 'No lo modifique con el editor de código. Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents dgCat As System.Windows.Forms.DataGrid Friend WithEvents dgprod As System.Windows.Forms.DataGrid Friend WithEvents cmdAnterior As System.Windows.Forms.Button Friend WithEvents cmdSiguiente As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Label1 = New System.Windows.Forms.Label Me.dgCat = New System.Windows.Forms.DataGrid Me.dgprod = New System.Windows.Forms.DataGrid Me.cmdAnterior = New System.Windows.Forms.Button Me.cmdSiguiente = New System.Windows.Forms.Button CType(Me.dgCat, System.ComponentModel.ISupportInitialize).BeginInit() CType(Me.dgprod, System.ComponentModel.ISupportInitialize).BeginInit() Me.SuspendLayout() ' 'Label1 ' Me.Label1.AutoSize = True Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(16, 16) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(470, 22) Me.Label1.TabIndex = 0 Me.Label1.Text = "Maestro - Detalle - Categorias - Productos usando Northwind" ' 'dgCat ' Me.dgCat.DataMember = "" Me.dgCat.HeaderForeColor = System.Drawing.SystemColors.ControlText Me.dgCat.Location = New System.Drawing.Point(16, 48) Me.dgCat.Name = "dgCat" Me.dgCat.ReadOnly = True Me.dgCat.Size = New System.Drawing.Size(664, 136) Me.dgCat.TabIndex = 1 ' 'dgprod ' Me.dgprod.DataMember = "" Me.dgprod.HeaderForeColor = System.Drawing.SystemColors.ControlText Me.dgprod.Location = New System.Drawing.Point(16, 232) Me.dgprod.Name = "dgprod" Me.dgprod.ReadOnly = True Me.dgprod.Size = New System.Drawing.Size(664, 208) Me.dgprod.TabIndex = 2 ' 'cmdAnterior ' Me.cmdAnterior.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.cmdAnterior.Location = New System.Drawing.Point(584, 192) Me.cmdAnterior.Name = "cmdAnterior" Me.cmdAnterior.Size = New System.Drawing.Size(32, 32) Me.cmdAnterior.TabIndex = 5 Me.cmdAnterior.Text = "<" ' 'cmdSiguiente ' Me.cmdSiguiente.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.cmdSiguiente.Location = New System.Drawing.Point(616, 192) Me.cmdSiguiente.Name = "cmdSiguiente" Me.cmdSiguiente.Size = New System.Drawing.Size(32, 32) Me.cmdSiguiente.TabIndex = 6 Me.cmdSiguiente.Text = ">" ' 'VBCategProductos ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(696, 453) Me.Controls.Add(Me.cmdSiguiente) Me.Controls.Add(Me.cmdAnterior) Me.Controls.Add(Me.dgprod) Me.Controls.Add(Me.dgCat) Me.Controls.Add(Me.Label1) Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle Me.MaximizeBox = False Me.MinimizeBox = False Me.Name = "VBCategProductos" Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen Me.Text = "Net Desarrollo SAC - Ing. Fernando Luque Sánchez - Aplicación en N Capas" CType(Me.dgCat, System.ComponentModel.ISupportInitialize).EndInit() CType(Me.dgprod, System.ComponentModel.ISupportInitialize).EndInit() Me.ResumeLayout(False) End Sub #End Region Dim dsDatos1 As DataSet 'Definir dos DataTables Dim dtCat As DataTable Dim dtProd As DataTable 'Las Vistas Dim dvCat As DataView Dim dvProd As DataView 'Instanciar la clase de acceso a datos Protected WithEvents cAcceso As VBNCapasAcceso 'PROCEDIMIENTO AL CARGAR EL FORMULARIO Private Sub Cargar(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load 'Instanciar la clase cAcceso = New VBNCapasAcceso 'Obtener el DataSet de la Clase VBNCapasAcceso dsDatos1 = cAcceso.CrearDataSet 'Origen de los DataTable dtCat = dsDatos1.Tables("Categorias") dtProd = dsDatos1.Tables("Productos") 'Origen de las Vistas dvCat = dtCat.DefaultView dvProd = dtProd.DefaultView 'Origen de los Grid dgCat.DataSource = dvCat dgCat.CaptionText = "Categorias" dgCat.Select(0) LlenarGridProductos() EstilosGrid() 'Solamente para las Categorías...por espacio y tiempo End Sub Private Sub LlenarGridProductos() Dim vID As String = dvCat(dgCat.CurrentRowIndex)("CategoryID").ToString dvProd.RowFilter = "CategoryID = " & vID dgprod.DataSource = dvProd dgprod.CaptionText = "Productos" End Sub Private Sub EstilosGrid() ' Limpiar el estilo del Grid dgCat.TableStyles.Clear() 'Estilo del Grid Dim EstiloGrid As New DataGridTableStyle With EstiloGrid .MappingName = dvCat.Table.TableName End With 'ESTILOS DE CADA COLUMNA Dim Estilo1 As New DataGridTextBoxColumn With Estilo1 .MappingName = "CategoryID" .HeaderText = "Id. de Categoría" .Width = 100 End With Dim Estilo2 As New DataGridTextBoxColumn With Estilo2 .MappingName = "CategoryName" .HeaderText = "Categoría" .Width = 180 End With Dim Estilo3 As New DataGridTextBoxColumn With Estilo3 .MappingName = "Description" .HeaderText = "Descripción" .Width = 300 End With ' Añadir el estilo EstiloGrid.GridColumnStyles.AddRange _ (New DataGridColumnStyle() {Estilo1, Estilo2, _ Estilo3}) dgCat.TableStyles.Add(EstiloGrid) End Sub 'PROCEDIMIENTO PARA MOVER ENTRE REGISTROS Private Sub Mover(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdAnterior.Click, cmdSiguiente.Click If sender Is cmdAnterior Then dgCat.UnSelect(dgCat.CurrentRowIndex) If dgCat.CurrentRowIndex > 0 Then dgCat.CurrentRowIndex -= 1 End If dgCat.Select(dgCat.CurrentRowIndex) ElseIf sender Is cmdSiguiente Then dgCat.UnSelect(dgCat.CurrentRowIndex) dgCat.CurrentRowIndex += 1 dgCat.Select(dgCat.CurrentRowIndex) End If LlenarGridProductos() End Sub Private Sub SeleccionarCat(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles dgCat.Click dgCat.Select(dgCat.CurrentRowIndex) LlenarGridProductos() End Sub Private Sub CambiarCeldaCat(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles dgCat.CurrentCellChanged dgCat.Select(dgCat.CurrentRowIndex) LlenarGridProductos() End Sub End ClassThere is other project called VBNCapasAcceso, the code is the following
Imports System.Data Imports System.Data.SqlClient Public Class VBNCapasAcceso 'Capa de Acceso a Datos 'Definición del String de Cadena de conexión Protected Const sCN As String = "data source=.; initial catalog=Northwind; integrated security=true" '"user id=sa;initial catalog=Northwind;persist security info=False" 'Los Adaptadores Private daCat As SqlDataAdapter Private daProd As SqlDataAdapter 'Declarar eventos que proveen información a la Capa de Presentación Public Event DataSetCreado(ByVal vCreado As Boolean) 'Creamos una función para retornar el DataSet Public Function CrearDataSet() As DataSet Dim dsDatos As DataSet Dim cn As New SqlConnection(sCN) 'Las instancias de los Adaptadores daCat = New SqlDataAdapter("Select CategoryId, CategoryName, Description from Categories", cn) daProd = New SqlDataAdapter("Select * from Products", cn) dsDatos = New DataSet daCat.Fill(dsDatos, "Categorias") daProd.Fill(dsDatos, "Productos") 'Indica que el evento de creación del DataSet fue exitoso RaiseEvent DataSetCreado(True) 'Retorna el DataSet Return dsDatos End Function End ClassBoth are in the solution called VBNCapas