Foros del Web » Programación para mayores de 30 ;) » .NET »

Añadir datos a un datagridview

Estas en el tema de Añadir datos a un datagridview en el foro de .NET en Foros del Web. Hola, os cuento: quiero hacer que un combo que yo relleno con los nombre de un cliente de la tabla tbl_clientes me devuelva el idcliente ...
  #1 (permalink)  
Antiguo 07/06/2010, 04:54
Avatar de Sergio18  
Fecha de Ingreso: abril-2010
Ubicación: No quieras saberlo
Mensajes: 94
Antigüedad: 14 años
Puntos: 0
Añadir datos a un datagridview

Hola,
os cuento:
quiero hacer que un combo que yo relleno con los nombre de un cliente de la tabla tbl_clientes me devuelva el idcliente de la tabla tbl_pagosdevoluciones, los campos son:

Esta todo en clases, ClsDatosClientes,ClsPagosDevoluciones y el formulario FrmPagosDevoluciones, despues en otra clase tengo la conexion (ClsBaseAccess)

tbl_clientes:Idcliente,nombre,apellidos,direccion,provincia,ciudad,telef ono,cp(codigopostal)

tbl_pagosdevoluciones: IdPagos,Importe,Idcliente,Descripcion,fecha

Los campos idcliente esta relacionados en la base acces.

El combo lo cargo de esta manera:

Código vb:
Ver original
  1. Public Function CargarComboClientes() As OleDb.OleDbDataAdapter
  2.  
  3. Dim CCargar As New OleDb.OleDbDataAdapter("Select * from tbl_clientes order by nombre", _cnClientes)
  4.  
  5. Return CCargar
  6.  
  7. End Function
  8.  
  9. Private Sub FrmPagosDevoluciones_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  10.  
  11. Dim dst As New DataSet
  12. Dim ds As New ClsDatosClientes
  13. Dim cc As New ClsPagosDevoluciones
  14. Dim dt As New DataTable
  15.  
  16. dst = ds.CargarTablaPagos
  17. Grid1.DataSource = dst.Tables(0)
  18.  
  19. cc.CargarComboClientes.Fill(dt)
  20. CbCliente.DataSource = dt
  21. CbCliente.DisplayMember = "Nombre"
  22. CbCliente.ValueMember = "IdCliente"
  23. CbCliente.Text = ""
  24.  
  25. End Sub

Y ahora para agregar los registros:

Código vb:
Ver original
  1. Public Function GuardarPagos(ByVal Pagos As ClsPagosDevoluciones) As String
  2.  
  3. Dim CGuardar As New OleDb.OleDbCommand
  4.  
  5. CGuardar.Connection = _cnClientes
  6.  
  7. CGuardar.CommandText = "Insert into tbl_pagosdevoluciones(Importe,IdCliente,Descripcio n, fecha)" & _
  8. "values " & _
  9. "(@importe,@idcliente,@descripcion,@fecha)"
  10.  
  11. CGuardar.Parameters.AddWithValue("@importe", Pagos.Importe)
  12. CGuardar.Parameters.AddWithValue("@Idcliente", Pagos.IdCliente)
  13. CGuardar.Parameters.AddWithValue("@Descripcion", Pagos.Descripcion)
  14. CGuardar.Parameters.AddWithValue("@fecha", Pagos.Fecha)
  15.  
  16. _cnClientes.Open()
  17.  
  18. Dim t As Integer = CInt(CGuardar.ExecuteScalar())
  19.  
  20. _cnClientes.Close()
  21.  
  22.  
  23. Return t
  24. End Function
  25.  
  26. Public Sub GuardarCampos()
  27.  
  28. Dim ds As New ClsDatosClientes
  29. ds.GuardarPagos(Me)
  30.  
  31. End Sub
  32.  
  33. Private Sub btnAgregarReg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregarReg.Click
  34.  
  35. Dim ds As New ClsDatosClientes
  36. Dim CGuardar As New ClsPagosDevoluciones
  37.  
  38. Try
  39. If MsgBox("¿Desea guardar los datos?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
  40.  
  41. CGuardar.Importe = txtImporte.Text
  42. CGuardar.IdCliente = CbCliente.Text
  43. CGuardar.Descripcion = txtDescripcion.Text
  44. CGuardar.Fecha = DTFecha.Text
  45. CGuardar.GuardarCampos()
  46.  
  47. Grid1.DataSource = ds.CargarTablaPagos.Tables(0)
  48.  
  49. End If
  50.  
  51. Catch ex As Exception
  52. MsgBox("Error:" & ex.Message)
  53. End Try
  54.  
  55. End Sub

Cuando le doy al boton agregar me salta un error en la funcion GuardarPagos: Dim t As Integer = CInt(CGuardar.ExecuteScalar()) no coinciden los datos en la expresion de criterios.

Espero que me hayan entendido y me puedan ayudar.

Gracias y Saludos

Etiquetas: añadir, datagridview
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 08:29.