AGREGAR,ACTUALIZAR,ELIMINAR Y MODIFICAR REGISTROS EN UN DATAGRIDVIEW-1.txt
-
Upload
david-osorio -
Category
Documents
-
view
90 -
download
0
Transcript of AGREGAR,ACTUALIZAR,ELIMINAR Y MODIFICAR REGISTROS EN UN DATAGRIDVIEW-1.txt
7/16/2019 AGREGAR,ACTUALIZAR,ELIMINAR Y MODIFICAR REGISTROS EN UN DATAGRIDVIEW-1.txt
http://slidepdf.com/reader/full/agregaractualizareliminar-y-modificar-registros-en-un-datagridview-1txt 1/4
Option Explicit OnOption Strict On
' Espacios de nombres' '''''''''''''''''''''''''''''''''''''''''Imports System.Data.SqlClient
Public Class Form1
'BindingSourcePrivate WithEvents bs As New BindingSource
' Adaptador de datos sqlPrivate SqlDataAdapter As SqlDataAdapter
' Cadena de conexiónPrivate Const cs As String = "Data Source=SERVIDOR;" & _
"Initial Catalog=BASE DE DATOS;" & _ "User Id=USUARIO;Password=CLAVE"
' flagPrivate bEdit As Boolean
' actualizar los cambios al salir' ''''''''''''''''''''''''''''''''''''''''Private Sub Form1_FormClosing( _
ByVal sender As Object, _ ByVal e As System.Windows.Forms.FormClosingEventArgs)
TryIf bEdit Then
'preguntar si se desea guardarIf (MsgBox( _
"Guardar cambios ?", _ MsgBoxStyle.YesNo, _
"guardar")) = MsgBoxResult.Yes Then
Actualizar(False)End If
End IfCatch ex As Exception
MessageBox.Show(ex.Message)End Try
End Sub
Private Sub cargar_registros( _
ByVal sql As String, _ ByVal dv As DataGridView)
Try' Inicializar el SqlDataAdapter indicandole el comando y el connecti
on stringSqlDataAdapter = New SqlDataAdapter(sql, cs)
Dim SqlCommandBuilder As New SqlCommandBuilder(SqlDataAdapter)
7/16/2019 AGREGAR,ACTUALIZAR,ELIMINAR Y MODIFICAR REGISTROS EN UN DATAGRIDVIEW-1.txt
http://slidepdf.com/reader/full/agregaractualizareliminar-y-modificar-registros-en-un-datagridview-1txt 2/4
' llenar el DataTableDim dt As New DataTable()SqlDataAdapter.Fill(dt)
' Enlazar el BindingSource con el datatable anterior' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''bs.DataSource = dt
With dv.Refresh()' coloca el registro arriba de todo.FirstDisplayedScrollingRowIndex = bs.Position
End With
bEdit = False
Catch exSql As SqlExceptionMsgBox(exSql.Message.ToString)
Catch ex As ExceptionMsgBox(ex.Message.ToString)
End TryEnd Sub
' botón para guardar los cambios y llenar la grilla
Private Sub Button1_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btn_Update.ClickTry
Actualizar()Catch ex As Exception
MessageBox.Show(ex.Message)End Try
End Sub
' Eliminar el elemento actual del BindingSource y actualizarPrivate Sub btn_delete_Click( _
ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btn_delete.ClickTry
If Not bs.Current Is Nothing Then' eliminarbs.RemoveCurrent()
'Guardar los cambios y recargarActualizar()
ElseMsgBox("No hay un registro actual para eliminar", _
MsgBoxStyle.Exclamation, _ "Eliminar")
End IfCatch ex As Exception
MessageBox.Show(ex.Message)End Try
End Sub
Private Sub Actualizar(Optional ByVal bCargar As Boolean = True)' Actualizar y guardar cambiosTry
7/16/2019 AGREGAR,ACTUALIZAR,ELIMINAR Y MODIFICAR REGISTROS EN UN DATAGRIDVIEW-1.txt
http://slidepdf.com/reader/full/agregaractualizareliminar-y-modificar-registros-en-un-datagridview-1txt 3/4
If Not bs.DataSource Is Nothing ThenSqlDataAdapter.Update(CType(bs.DataSource, DataTable))If bCargar Then
cargar_registros("" & TXT_FILTRO.Text & "", DataGridView1)End If
End IfCatch ex As Exception
MessageBox.Show(ex.Message)End Try
End Sub
Private Sub btn_first_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _
Handles btn_first.Click, btn_last.Click, btn_next.Click, btn_Previous.Click
' Botones para moverse por los registros' '''''''''''''''''''''''''''''''''''''''''''''Try
If sender Is btn_Previous Thenbs.MovePrevious()
ElseIf sender Is btn_first Thenbs.MoveFirst()
ElseIf sender Is btn_next Thenbs.MoveNext()ElseIf sender Is btn_last Then
bs.MoveLast()End If
Catch ex As ExceptionMessageBox.Show(ex.Message)
End Try
End Sub
Private Sub DataGridView1_CellEndEdit( _
ByVal sender As Object, _ ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _ Handles DataGridView1.CellEndEdit
bEdit = TrueEnd Sub
' nuevo registroPrivate Sub btn_new_Click( _
ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btn_new.ClickTry
bs.AddNew()
Catch ex As ExceptionMessageBox.Show(ex.Message)
End TryEnd Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' propiedades del datagrid
7/16/2019 AGREGAR,ACTUALIZAR,ELIMINAR Y MODIFICAR REGISTROS EN UN DATAGRIDVIEW-1.txt
http://slidepdf.com/reader/full/agregaractualizareliminar-y-modificar-registros-en-un-datagridview-1txt 4/4
' '''''''''''''''''''''''''''''''''''''Try
With DataGridView1' alternar color de filas.AlternatingRowsDefaultCellStyle.BackColor = Color.FloralWhite.DefaultCellStyle.BackColor = Color.Beige' Establecer el origen de datos para el DataGridview.DataSource = bs
End With
' botones' '''''''''''''''''''''''''''''''''''''btn_Update.Text = "Guardar cambios"btn_delete.Text = "Eliminar registro"btn_new.Text = "Nuevo"
btn_first.Text = "<<"btn_Previous.Text = "<"btn_next.Text = ">"btn_last.Text = ">>"
' cagar los datos' cargar_registros("Select * From dbo.inv_Articulos Order by CODIGO
", DataGridView1)
Catch ex As ExceptionMessageBox.Show(ex.Message)End Try
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Trycargar_registros("" & TXT_FILTRO.Text & "", DataGridView1)
Catch ex As ExceptionMessageBox.Show(ex.Message)
End TryEnd Sub
End Class