none
Guardar varias filas con DataGridView RRS feed

  • Pregunta

  • Hola buenos días tengo una consulta

    Miren de esta forma ingreso datos mediante un DataGridView a una base de datos, pero de esta forma solo puedo ingresar fila por fila y me gustaría hacerlo de varias filas a la vez. Como podría hacerlo??

    Private Sub btnokfactores_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnokfactores.Click

             Try

                conectado()

                    Try

                        Dim x = DataGridView1.Rows.Count - 2

                        cmd = New SqlCommand("INSERT INTO Factores (cod, des, val, mod, red_res, fec_ini)"&_

                                            " VALUES (@dt1, @dt2, @dt3, @dt4, @dt5, @dt6); ")

                        cmd.CommandType = CommandType.Text

                        cmd.Connection = cnn

                        cmd.Parameters.AddWithValue("@dt1", DataGridView1.Rows(x).Cells(0).Value())

                        cmd.Parameters.AddWithValue("@dt2", DataGridView1.Rows(x).Cells(1).Value())

                        cmd.Parameters.AddWithValue("@dt3", DataGridView1.Rows(x).Cells(2).Value())

                        cmd.Parameters.AddWithValue("@dt4", DataGridView1.Rows(x).Cells(3).Value())

                        cmd.Parameters.AddWithValue("@dt5", DataGridView1.Rows(x).Cells(4).Value())

                        cmd.Parameters.AddWithValue("@dt6", DataGridView1.Rows(x).Cells(5).Value())

                        cmd.ExecuteNonQuery()

                        MsgBox("Datos ingresados")

                    Catch ex As Exception

                        MsgBox("Error al insertar Factores")

                        MsgBox(ex.Message)

                    Finally

                        desconectado()

                    End Try

        End Sub


    lunes, 9 de abril de 2012 13:18

Respuestas

  • Este metodo me dio resultado

      cmd = New SqlCommand("SELECT COUNT(*) FROM impuestos ")
                    cmd.CommandType = CommandType.Text
                    cmd.Connection = cnn
                    cmd.ExecuteNonQuery()
                    Dim x As Integer = CInt(cmd.ExecuteScalar())
                    For Each row As DataGridViewRow In DataGridView4.Rows
                        If Not String.IsNullOrEmpty(DataGridView4.Rows(x).Cells(0).Value) Then
                            Try
                                conectado()
                                cmd = New SqlCommand("INSERT INTO impuestos (cod, des, porcent, cod_sii, comp_sob)" & _
                                                   " VALUES (@dt1, @dt2, @dt3, @dt4, @dt5); ")
                                cmd.CommandType = CommandType.Text
                                cmd.Connection = cnn
                                cmd.Parameters.AddWithValue("@dt1", DataGridView4.Rows(x).Cells(0).Value())
                                cmd.Parameters.AddWithValue("@dt2", DataGridView4.Rows(x).Cells(1).Value())
                                cmd.Parameters.AddWithValue("@dt3", DataGridView4.Rows(x).Cells(2).Value())
                                cmd.Parameters.AddWithValue("@dt4", DataGridView4.Rows(x).Cells(3).Value())
                                cmd.Parameters.AddWithValue("@dt5", DataGridView4.Rows(x).Cells(4).Value())
                                cmd.ExecuteNonQuery()
                            Catch ex As Exception
                                MsgBox("Error al insertar Impuestos")
                                MsgBox(ex.Message)
                            Finally
                                desconectado()
                            End Try
                            x = x + 1
                            Continue For
                        End If
                    Next
                    MsgBox("Datos Guardados")
                End If

    lunes, 9 de abril de 2012 20:01

Todas las respuestas

  • podrias lograrlo  por medio de un datatable

    Cómo: Enlazar datos al control DataGridView de formularios Windows Forms

    como veras defines al dataadpater con las instrucciones de INSERT o UPDATE y luego envias en el Update() el datatable con los cambios en sus registrod

    saludos


    Leandro Tuttini

    Blog
    Buenos Aires
    Argentina

    lunes, 9 de abril de 2012 13:41
  • Este metodo me dio resultado

      cmd = New SqlCommand("SELECT COUNT(*) FROM impuestos ")
                    cmd.CommandType = CommandType.Text
                    cmd.Connection = cnn
                    cmd.ExecuteNonQuery()
                    Dim x As Integer = CInt(cmd.ExecuteScalar())
                    For Each row As DataGridViewRow In DataGridView4.Rows
                        If Not String.IsNullOrEmpty(DataGridView4.Rows(x).Cells(0).Value) Then
                            Try
                                conectado()
                                cmd = New SqlCommand("INSERT INTO impuestos (cod, des, porcent, cod_sii, comp_sob)" & _
                                                   " VALUES (@dt1, @dt2, @dt3, @dt4, @dt5); ")
                                cmd.CommandType = CommandType.Text
                                cmd.Connection = cnn
                                cmd.Parameters.AddWithValue("@dt1", DataGridView4.Rows(x).Cells(0).Value())
                                cmd.Parameters.AddWithValue("@dt2", DataGridView4.Rows(x).Cells(1).Value())
                                cmd.Parameters.AddWithValue("@dt3", DataGridView4.Rows(x).Cells(2).Value())
                                cmd.Parameters.AddWithValue("@dt4", DataGridView4.Rows(x).Cells(3).Value())
                                cmd.Parameters.AddWithValue("@dt5", DataGridView4.Rows(x).Cells(4).Value())
                                cmd.ExecuteNonQuery()
                            Catch ex As Exception
                                MsgBox("Error al insertar Impuestos")
                                MsgBox(ex.Message)
                            Finally
                                desconectado()
                            End Try
                            x = x + 1
                            Continue For
                        End If
                    Next
                    MsgBox("Datos Guardados")
                End If

    lunes, 9 de abril de 2012 20:01