Insert Every record of a datagridview based on visual studio 2015 data?

1

I have a big question about ... How do I make multiple entries into my database the records that are inside my datagridview?.

for example: A trip of such date, insert all the people that will go to that trip, in this case they would be all those that are in my datagridview.

I've been testing with For and datagridview.RowCount but without success.

Since thank you very much !!!.

    
asked by Sergio Aguiriano 04.10.2017 в 09:05
source

2 answers

0

Walk the DataGridView and see collecting the values to your liking.

For Each row As DataGridViewRow In BOM.dvg_retiro.Rows
  For i = 0 To row.Cells.Count - 1
      nombre = fila.Cells(i).Value 
      TextBox1.Text = Convert.ToString(nombre) 
  Next
Next

I leave a link where you can see it better: Loop through DataGridView

    
answered by 04.10.2017 в 09:22
0

Good Sergio,

I leave the code with a For Each going through all the rows of the DataGridView to insert the data in your database opening only the connection once:

Using cn As New SqlConnection("cadena_conexión")
    Using cm As New SqlCommand()
        cm.Connection = cn
        cn.Open()
        For Each dr As DataGridViewRow In dvg_retiro.Rows
            cm.CommandText = " INSERT INTO nombre_tabla (col1, col2, col3) VALUES (" & _
                dr.Cells("nombre_columna1").Value & "," & dr.Cells("nombre_columna2").Value & "," & dr.Cells("nombre_columna3").Value & ")"
            cm.ExecuteNonQuery()
        Next
        cn.Close()
    End Using
End Using
    
answered by 04.10.2017 в 09:30