Vous êtes sur la page 1sur 5

MICROSOFT VISUAL BASIC .

NET
Prof. Miguel Cherres Orihuela
mcherres@hotmail.com

-1-

MICROSOFT VISUAL BASIC .NET


Prof. Miguel Cherres Orihuela
mcherres@hotmail.com

-2-

MICROSOFT VISUAL BASIC .NET


Prof. Miguel Cherres Orihuela
mcherres@hotmail.com

-3-

MICROSOFT VISUAL BASIC .NET


Prof. Miguel Cherres Orihuela
mcherres@hotmail.com
Imports System.Data
Imports System.Data.SqlClient
Public Class form1
Public cn As New SqlConnection
Public da1 As SqlDataAdapter
Public ds1 As New DataSet
Public dv1 As New DataView
Public objcm As SqlCommandBuilder
Public fila As DataRow
Dim indice As Integer
Dim totr As Integer
Dim nr As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
cn = New SqlConnection("data source =.;initial catalog=bdventas;integrated Security=true")
'cn = New SqlConnection("server=(local);uid=sa;database=bdventas")'
cn.Open()
da1 = New SqlDataAdapter("select codigo,nombre,edad,sueldo from vendedor", cn)
objcm = New SqlCommandBuilder(da1)
da1.Fill(ds1, "vendedor")
dv1 = ds1.Tables("vendedor").DefaultView
dv1.AllowNew = True
dv1.AllowEdit = True
dv1.AllowDelete = True
DataGridView1.DataSource = dv1
nr = ds1.Tables(0).DefaultView.Count
indice = 0
If nr > 0 Then
selecciona(btnprimero)
End If
End Sub
Sub selecciona(ByVal btn As Button)
'mostrar nro de registro
Dim totr As Integer = nr - 1
If btn Is btnprimero Then
indice = 0
ElseIf btn Is btnanterior Then
indice -= 1
If indice > totr Then indice = totr
ElseIf btn Is btnsiguiente Then
indice += 1
If indice > totr Then indice = 0
Else
indice = totr
End If
muestradatos(indice)
TextBox5.Text = (indice + 1).ToString & "/" & nr
End Sub
Sub muestradatos(ByVal nro As Integer)
'mostrar registro
fila = ds1.Tables("vendedor").Rows(nro)
TextBox1.Text = fila.Item(0)
TextBox2.Text = fila.Item(1)
TextBox3.Text = fila.Item(2)
TextBox4.Text = fila.Item(3)
End Sub
Sub asigna()
'asinar intems
fila.Item(0) = TextBox1.Text
fila.Item(1) = TextBox2.Text
fila.Item(2) = TextBox3.Text
fila.Item(3) = TextBox4.Text

-4-

MICROSOFT VISUAL BASIC .NET


Prof. Miguel Cherres Orihuela
mcherres@hotmail.com
End Sub
Private Sub btnnuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnnuevo.Click
Dim x As Control
Dim xcod As String
For Each x In Me.Controls
If TypeOf x Is TextBox Then x.Text = ""
Next
totr = ds1.Tables(0).DefaultView.Count
If totr > 0 Then
xcod = ds1.Tables(0).Rows(nr - 1).Item(0)
xcod = "v" + Format(Val(xcod.Substring(1, 3)) + 1, "000")
Else
xcod = "v001"
End If
TextBox1.Text = xcod
TextBox2.Focus()
End Sub
Private Sub btnguardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnguardar.Click
'guardar registro
fila = ds1.Tables(0).NewRow
asigna()
ds1.Tables(0).Rows.Add(fila)
da1.Update(ds1.Tables("vendedor"))
nr = ds1.Tables(0).DefaultView.Count
selecciona(btnultimon)
End Sub
Private Sub btncancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btncancelar.Click
Dim x As Control
For Each x In Me.Controls
If TypeOf x Is TextBox Then x.Text = ""
Next
End Sub
Private Sub btneliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btneliminar.Click
If indice > 0 Then
dv1.Delete(indice)
da1.Update(ds1.Tables("vendedor"))
nr = ds1.Tables(0).DefaultView.Count()
selecciona(btnultimon)
End If
End Sub
Private Sub handlesclick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnprimero.Click, btnanterior.Click, btnsiguiente.Click, btnultimon.Click
selecciona(CType(sender, Button))
End Sub
End Class

-5-

Vous aimerez peut-être aussi