Estroy trabajando en capas
En la capa presentación tengo los formularios:
menuprincipal - MDI
listacategoria
Registrarcategoria
Lo que pasa es que al insertar un nuevo registro no me actualiza o no me refresca el datagridview para ver el registro insertado; acabo de ver en los foros y hay unos que utilizan
Código vb:
Pero no me parece lo mejor en fin por ahi alguna idea de como puedo lograr esoVer original
form.showdialog cargarlista
les dejo el codigo
1*CApaAccesodatos
clase: CategoriaAD
Código vb:
Ver original
Imports System.Data Imports Npgsql Imports NpgsqlTypes imports EntidadNegocio Public Class CategoriaAD Private conn As New NpgsqlConnection Private comando As New NpgsqlCommand #Region "Cadena de conexion" Public Sub New() Dim Objconexion As New conexion conn = Objconexion.abrir comando.Connection=conn End Sub #End Region #Region "Función convertir datos" Private Shared Function Convertircategoria(ByVal reader As IDataReader) As CategoriaEN Dim categoria As New CategoriaEN() categoria.gidcat = Convert.ToInt32(reader("idcat")) categoria.gnom_cat = lTrim(RTrim(Convert.ToString(reader("nom_cat")))) categoria.gdesc_cat = LTrim(RTrim(Convert.ToString(reader("desc_cat")))) Return categoria End Function #End Region #Region "Función listar" Public Function listado() As List(Of CategoriaEN) Dim listcat As New List(Of CategoriaEN) Dim reader As NpgsqlDataReader Dim Objcat As CategoriaEN Try conn.Close() conn.Open() comando.CommandType=CommandType.Text comando.CommandText="SELECT idcat,nom_cat,desc_cat FROM categoria order by nom_cat,idcat asc" reader=comando.ExecuteReader While reader.Read() listcat.Add(Convertircategoria(reader)) End While Return listcat Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Critical, ex.Source) Finally conn.Close() conn.ClearAllPools End Try End Function #End Region #Region "Función insertar" Public Sub agregar(ByVal ObjCategoriaEN As CategoriaEN) Try comando.CommandType=CommandType.StoredProcedure comando.CommandText="agrega_categ" Dim _nom_cat As New NpgsqlParameter("_nom_cat",NpgsqlTypes.NpgsqlDbType.Varchar) _nom_cat.Value=ObjcategoriaEN.gnom_cat _nom_cat.Direction=ParameterDirection.Input comando.Parameters.Add(_nom_cat) Dim _desc_cat As New NpgsqlParameter("_desc_cat",NpgsqlTypes.NpgsqlDbType.Text) _desc_cat.Value=ObjCategoriaEN.gdesc_cat _desc_cat.Direction=ParameterDirection.Input comando.Parameters.Add(_desc_cat) comando.ExecuteNonQuery() Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Critical, ex.Source) Finally conn.Close() conn.ClearAllPools End Try End Sub #End Region End Class
*2.- Capa: EntidadNegocio
clase: CategoriaEN
Código vb:
Ver original
Public Class CategoriaEN Private idcat As Integer Private nom_cat As String Private desc_cat As String '-------------------------- Public Property gidcat() Get Return idcat End Get Set(ByVal value) idcat=value End Set End Property '-------------------------- Public Property gnom_cat() Get Return nom_cat End Get Set(ByVal value) nom_cat=value End Set End Property '-------------------------- Public Property gdesc_cat() Get return desc_cat End Get Set(ByVal value) desc_cat=value End Set End Property '-------------------------- End Class
*3.- Capa: LogicaNegocio
Clase: CategoriaLN
Código vb:
Ver original
Imports AccessoDatos imports EntidadNegocio Public Class CategoriaLN Private ObjCategoriaAD As CategoriaAD Public Sub New() ObjCategoriaAD=New CategoriaAD End Sub Public Sub agregar(ByVal ObjCategoriaEN As CategoriaEN) ObjCategoriaAD.agregar(ObjCategoriaEN) End Sub Public Function listado() As List(Of CategoriaEN) return ObjCategoriaAD.listado End Function End Class
*4.- Capa: Presentacion
Form: listcategoria
Código vb:
Ver original
Imports System.Data Imports EntidadNegocio Imports LogicaNegocio Public Partial Class listcategoria Public Sub New() ' The Me.InitializeComponent call is required for Windows Forms designer support. Me.InitializeComponent() ' ' TODO : Add constructor code after InitializeComponents ' End Sub Private ObjCategoriaEN As CategoriaEN Private ObjCategoriaLN As CategoriaLN Public Sub cargarlista() Try ObjCategoriaEN=New CategoriaEN ObjCategoriaLN=New CategoriaLN dgwlistcategoria.DataSource=Me.ObjCategoriaLN.listado Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Critical, ex.Source) End Try End Sub Sub ListcategoriaLoad(sender As Object, e As EventArgs) cargarlista() End Sub Sub Button2Click(sender As Object, e As EventArgs) frm_insert_cat.Show() End Sub Sub Button5Click(sender As Object, e As EventArgs) cargarlista() End Sub End Class
Form: frm_insert_cat
Código vb:
Ver original
imports System.Windows.Forms Imports EntidadNegocio Imports LogicaNegocio Public Partial Class frm_insert_cat Public Sub New() ' The Me.InitializeComponent call is required for Windows Forms designer support. Me.InitializeComponent() ' ' TODO : Add constructor code after InitializeComponents ' End Sub Private ObjCategoriaEN As CategoriaEN Private ObjCategoriaLN As CategoriaLN Sub Button2Click(sender As Object, e As EventArgs) me.Close() End Sub Sub Button1Click(sender As Object, e As EventArgs) Dim listcat As New listcategoria Try ObjCategoriaEN=New CategoriaEN ObjCategoriaLN=New CategoriaLN ObjCategoriaEN.gnom_cat=Ltrim(Rtrim(txtnom_cat.Text)) ObjCategoriaEN.gdesc_cat=Ltrim(Rtrim(txtdesc_cat.Text)) ObjCategoriaLN.agregar(ObjCategoriaEN) Listcat.cargarlista() MessageBox.Show("Se grabo correctamente") If MessageBox.Show("¿Desea Continuar agregando registros ?","Mensaje",MessageBoxButtons.YesNo,MessageBoxIcon.Question)=Windows.Forms.DialogResult.Yes Then txtnom_cat.Text="" txtdesc_cat.Text="" txtnom_cat.Focus Else Me.close() End If Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Critical, ex.Source) End Try End Sub End Class
Como veran en el form: frm_insert_cat hago un llamado a cargarlista() pero no pasa nada alguien que me pueda orientar