Ver Mensaje Individual
  #5 (permalink)  
Antiguo 22/12/2009, 11:35
oslar
 
Fecha de Ingreso: marzo-2008
Mensajes: 81
Antigüedad: 16 años, 11 meses
Puntos: 0
Respuesta: trabajando con datagrid..ayuda

tras investigar por google di con un codigo que e conseguido adaptar a mi form,pero me da un error:
Código vb:
Ver original
  1. Imports System.Data.OleDb
  2.  
  3. Public Class Form6
  4.     Dim dt As New DataTable
  5.     Dim fila As Integer = 0
  6.     Dim da As New OleDb.OleDbDataAdapter
  7.     'BindingSource  
  8.    Private WithEvents bs As New BindingSource
  9.     ' Cadena de conexión  
  10.    Dim sCnn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & " Data Source=C:\negocio\negocio.mdb"
  11.     ' flag  
  12.    Private bEdit As Boolean
  13.  
  14.     ' actualizar los cambios al salir    
  15.    Private Sub Form6_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
  16.         If bEdit Then
  17.             'preguntar si se desea guardar  
  18.            If (MsgBox("Guardar cambios ?", MsgBoxStyle.YesNo, "guardar")) = MsgBoxResult.Yes Then
  19.                 Actualizar(False)
  20.             End If
  21.         End If
  22.     End Sub
  23.     Private Sub Form6_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  24.         ' propiedades del datagrid  
  25.        With DataGridView1
  26.             ' alternar color de filas  
  27.            .AlternatingRowsDefaultCellStyle.BackColor = Color.FloralWhite
  28.             .DefaultCellStyle.BackColor = Color.Beige
  29.             ' Establecer el origen de datos para el DataGridview  
  30.            .DataSource = bs
  31.         End With
  32.         ' cagar los datos  
  33.        cargar_registros("Select * From usuarios", DataGridView1)
  34.     End Sub
  35.  
  36.     Private Sub cargar_registros(ByVal sql As String, ByVal dv As DataGridView)
  37.  
  38.         Try
  39.             ' Inicializar el SqlDataAdapter indicandole el comando y el connection string  
  40.            Dim sq As String = "SELECT * FROM usuarios"
  41.             ' Crear un nuevo objeto del tipo DataAdapter
  42.            Dim da As New OleDb.OleDbDataAdapter(sq, sCnn)
  43.             da.Fill(dt)
  44.             ' Enlazar el BindingSource con el datatable anterior  
  45.            bs.DataSource = dt
  46.  
  47.             With dv
  48.                 .Refresh()
  49.                 ' coloca el registro arriba de todo  
  50.                .FirstDisplayedScrollingRowIndex = bs.Position
  51.             End With
  52.  
  53.             bEdit = False
  54.         Catch exSql As OleDbException
  55.             MsgBox(exSql.Message.ToString)
  56.         Catch ex As Exception
  57.             MsgBox(ex.Message.ToString)
  58.         End Try
  59.     End Sub
  60.     ' botón para guardar los cambios y llenar la grilla  
  61.    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
  62.         Actualizar()
  63.     End Sub
  64.     ' Eliminar el elemento actual del BindingSource y actualizar  
  65.    Private Sub buton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
  66.         If Not bs.Current Is Nothing Then
  67.             ' eliminar  
  68.            bs.RemoveCurrent()
  69.             'Guardar los cambios y recargar  
  70.            Actualizar()
  71.         Else
  72.             MsgBox("No hay un registro actual para eliminar", MsgBoxStyle.Exclamation, "Eliminar")
  73.         End If
  74.     End Sub
  75.     Private Sub Actualizar(Optional ByVal bCargar As Boolean = True)
  76.         ' Actualizar y guardar cambios  
  77.        If Not bs.DataSource Is Nothing Then
  78.             da.Update(CType(bs.DataSource, DataTable))   ' <<------aki me da el error
  79.            If bCargar Then
  80.                 cargar_registros("Select * From usuarios", DataGridView1)
  81.             End If
  82.         End If
  83.     End Sub
  84.     Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button4.Click, Button3.Click, Button2.Click
  85.         If sender Is Button2 Then
  86.             bs.MovePrevious()
  87.         ElseIf sender Is Button1 Then
  88.             bs.MoveFirst()
  89.         ElseIf sender Is Button3 Then
  90.             bs.MoveNext()
  91.         ElseIf sender Is Button4 Then
  92.             bs.MoveLast()
  93.         End If
  94.     End Sub
  95.     Private Sub DataGridView1_CellEndEdit( _
  96.         ByVal sender As Object, _
  97.         ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
  98.             Handles DataGridView1.CellEndEdit
  99.         bEdit = True
  100.     End Sub
  101.     ' nuevo registro  
  102.    Private Sub button5_Click( _
  103.         ByVal sender As System.Object, _
  104.         ByVal e As System.EventArgs) Handles Button5.Click
  105.         bs.AddNew()
  106.     End Sub
  107. 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