Inicio > Uncategorized > Consultar, Agregar y eliminar datos en Listview

Consultar, Agregar y eliminar datos en Listview

La clase que estoy usando se llama cpersona


Imports System.Data
Imports System.Data.SqlClient

Public Class cPersona

Private _idpersona As Integer
Private _nombres As String
Private _apellidos As String
Private _dni As String

Public Property idpersona() As Integer
Get
Return _idpersona

End Get
Set(ByVal value As Integer)
_idpersona = value

End Set
End Property

Public Property nombres() As String
Get
Return _nombres
End Get
Set(ByVal value As String)
_nombres = value

End Set
End Property

Public Property apellidos() As String
Get
Return _apellidos

End Get
Set(ByVal value As String)
_apellidos = value

End Set
End Property

Public Property dni() As String
Get
Return _dni

End Get
Set(ByVal value As String)
_dni = value

End Set
End Property

Public Function mostrar_personas()
Dim cmd As New SqlCommand
Dim dr As SqlDataReader

cmd.CommandText = "sp_mostrar"
cmd.Connection = Conexion.con
cmd.CommandType = CommandType.StoredProcedure

If Conexion.con.State = ConnectionState.Open Then Conexion.con.Close()
Conexion.con.Open()
dr = cmd.ExecuteReader
Return dr

dr.Close()
Conexion.con.Close()

End Function

Public Function combopersonas() As DataSet
Dim ds As New DataSet
Dim da As New SqlDataAdapter
da.SelectCommand = New SqlCommand
da.SelectCommand.CommandText = "comboprueba"
da.SelectCommand.CommandType = CommandType.StoredProcedure
da.SelectCommand.Connection = Conexion.con
Conexion.con.Open()
da.Fill(ds, "persona")
Conexion.con.Close()
Return ds

End Function

End Class

aqui esta el modulo de conexion

Imports System.Data.SqlClient

Module Conexion

Public cadena As String = "Data Source=SERVERX;DataBase=SISclinica;Integrated Security=True"
Public con As New SqlConnection(cadena)

End Module

aqui le dejo el codigo del formulario

Imports System.Data
Imports System.Data.SqlClient
Imports PruebaListView.Conexion

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable("persona")
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Conexion.con.Open()

cmd.Connection = Conexion.con
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "comboprueba"
dr = cmd.ExecuteReader()
dt.Load(dr)
ComboBox1.DataSource = dt
ComboBox1.ValueMember = "idpersona"
ComboBox1.DisplayMember = "nombres"

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim objpersona As cPersona
objpersona = New cPersona
Dim filab As ListViewItem
Dim drb As SqlDataReader
ListView1.Items.Clear()

drb = objpersona.mostrar_personas
Do While drb.Read = True

filab = New ListViewItem(CStr(drb.Item(0)))
filab.SubItems.Add(CStr(drb.Item(1)))
filab.SubItems.Add(CStr(drb.Item(2)))
filab.SubItems.Add(CStr(drb.Item(3)))

ListView1.Items.Add(filab)
filab = Nothing
Loop
drb.Close()

End Sub

Private Sub CToolBar1_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs)

End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Dim filac As ListViewItem
ListView1.Items.Clear()
cmd.Connection = Conexion.con
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "spllenarpersona"
cmd.Parameters.Add("@codigo", SqlDbType.Int)
cmd.Parameters("@codigo").Value = CStr(ComboBox1.SelectedValue.GetHashCode)

dr = cmd.ExecuteReader

Do While dr.Read = True
filac = New ListViewItem(CStr(dr.Item(0)))
filac.SubItems.Add(CStr(dr.Item(1)))
filac.SubItems.Add(CStr(dr.Item(2)))
filac.SubItems.Add(CStr(dr.Item(3)))

ListView1.Items.Add(filac)
filac = Nothing
Loop

dr.Close()

End Sub

Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
For Each elemento In ListView1.SelectedIndices
ListView1.Items.RemoveAt(elemento)
Next

End Sub
End Class

Aqui les dejo el material acabado

Categorías:Uncategorized
  1. Aún no hay comentarios.
  1. No trackbacks yet.

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

A %d blogueros les gusta esto: