tras investigar por google di con un codigo que e conseguido adaptar a mi form,pero me da un error:
Código vb:
Ver originalImports System.Data.OleDb
Public Class Form6
Dim dt As New DataTable
Dim fila As Integer = 0
Dim da As New OleDb.OleDbDataAdapter
'BindingSource
Private WithEvents bs As New BindingSource
' Cadena de conexión
Dim sCnn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & " Data Source=C:\negocio\negocio.mdb"
' flag
Private bEdit As Boolean
' actualizar los cambios al salir
Private Sub Form6_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If bEdit Then
'preguntar si se desea guardar
If (MsgBox("Guardar cambios ?", MsgBoxStyle.YesNo, "guardar")) = MsgBoxResult.Yes Then
Actualizar(False)
End If
End If
End Sub
Private Sub Form6_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' propiedades del datagrid
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
' cagar los datos
cargar_registros("Select * From usuarios", DataGridView1)
End Sub
Private Sub cargar_registros(ByVal sql As String, ByVal dv As DataGridView)
Try
' Inicializar el SqlDataAdapter indicandole el comando y el connection string
Dim sq As String = "SELECT * FROM usuarios"
' Crear un nuevo objeto del tipo DataAdapter
Dim da As New OleDb.OleDbDataAdapter(sq, sCnn)
da.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 OleDbException
MsgBox(exSql.Message.ToString)
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
End Sub
' botón para guardar los cambios y llenar la grilla
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
Actualizar()
End Sub
' Eliminar el elemento actual del BindingSource y actualizar
Private Sub buton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If Not bs.Current Is Nothing Then
' eliminar
bs.RemoveCurrent()
'Guardar los cambios y recargar
Actualizar()
Else
MsgBox("No hay un registro actual para eliminar", MsgBoxStyle.Exclamation, "Eliminar")
End If
End Sub
Private Sub Actualizar(Optional ByVal bCargar As Boolean = True)
' Actualizar y guardar cambios
If Not bs.DataSource Is Nothing Then
da.Update(CType(bs.DataSource, DataTable)) ' <<------aki me da el error
If bCargar Then
cargar_registros("Select * From usuarios", DataGridView1)
End If
End If
End Sub
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button4.Click, Button3.Click, Button2.Click
If sender Is Button2 Then
bs.MovePrevious()
ElseIf sender Is Button1 Then
bs.MoveFirst()
ElseIf sender Is Button3 Then
bs.MoveNext()
ElseIf sender Is Button4 Then
bs.MoveLast()
End If
End Sub
Private Sub DataGridView1_CellEndEdit( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
Handles DataGridView1.CellEndEdit
bEdit = True
End Sub
' nuevo registro
Private Sub button5_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button5.Click
bs.AddNew()
End Sub
End Class
Error:Update requiere que DeleteCommand sea válido cuando se pasa la colección DataRow con filas eliminadas.
Me da error al intentar borrar,editar o guardar los cambios
los botones ir al primero,ir al ultimo,siguiente anterior funcionan perfectamente
gracias por vuestro tiempo