You are on page 1of 5

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(sender As Object, e As EventArgs) Handles MyBase.Load


cn = New SqlConnection("data source=.;initial catalog=bdventas;integrated security=true")
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()

'asignar items

fila.Item(0) = TextBox1.Text

fila.Item(1) = TextBox2.Text

fila.Item(2) = TextBox3.Text

fila.Item(3) = TextBox4.Text

End Sub

---------------------------------------------------------------------------------
Private Sub btnnuevo_Click(sender As Object, e As EventArgs) Handles btnnuevo.Click
'agregar nuevo registro

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(sender As Object, e As 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(btnultimo)
End Sub
---------------------------------------------------------------------------------
Private Sub btncancelar_Click(sender As Object, e As 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(sender As Object, e As EventArgs) Handles btneliminar.Click
If indice > 0 Then

dv1.Delete(indice)

da1.Update(ds1.Tables("vendedor"))

nr = ds1.Tables(0).DefaultView.Count()

selecciona(btnultimo)

End If

End Sub

---------------------------------------------------------------------------------
Private Sub HandlesClick(sender As Object, e As EventArgs) Handles btnprimero.Click, btnanterior.Click,
btnsiguiente.Click, btnultimo.Click
selecciona(CType(sender, Button))

End Sub

---------------------------------------------------------------------------------
Private Sub SALIR (sender As Object, e As EventArgs) Handles btnsalir.Click
End
---------------------------------------------------------------------------------

End Class
create database bdventas

use bdventas

create table vendedor

(codigo char(4) not null primary key,

nombre varchar(30) not null,

edad int not null,

sueldo numeric(8,2) not null)

select * from vendedor

insert into vendedor values ('V001','Miguel Alonso',21,800)

insert into vendedor values ('V002','Reina Ysabel',18,1800)

insert into vendedor values ('V003','Gladys Elizabeth',25,700)

You might also like