Programming for Everybody: Update all rows
Showing posts with label Update all rows. Show all posts
Showing posts with label Update all rows. Show all posts

How to update All Data From Datagridview To Database In Vb.net [ with source code ]





Imports System.Data
Imports System.Data.SqlClient

Public Class Form2

    Dim conn1 As New SqlConnection("data source=.;initial catalog=users;integrated security=true")


    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        Dim table As New DataTable("table")



        table.Columns.Add("id", Type.GetType("System.Int32"))
        table.Columns.Add("Name", Type.GetType("System.String"))
        table.Columns.Add("Country", Type.GetType("System.String"))
        table.Columns.Add("id_group", Type.GetType("System.Int32"))


        Dim cmd As New SqlCommand("", conn1)

        With cmd

            .Connection = conn1

            .CommandText = "select id,name,country,id_group from table2 where id_group=" & Label1.Text & ""


        End With

        Dim da As New SqlDataAdapter
        da.SelectCommand = cmd

        Dim dt As New DataTable
        dt.Clear()
        da.Fill(dt)
        DataGridView1.DataSource = dt

        DataGridView1.AllowUserToAddRows = False

        DataGridView1.Columns(0).HeaderText = "ID"

        DataGridView1.Columns(1).HeaderText = "Name"


        DataGridView1.Columns(2).HeaderText = "Country"


        DataGridView1.Columns(3).HeaderText = "Id_group"








    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For i As Integer = 0 To DataGridView1.Rows.Count - 1

            Dim cmd4 As New SqlCommand("", conn1)

            cmd4.CommandText = "update table2 set name='" & DataGridView1.Rows(i).Cells(1).Value & "',country='" & DataGridView1.Rows(i).Cells(2).Value & "' where id=" & DataGridView1.Rows(i).Cells(0).Value & ""

            conn1.Open()
            cmd4.ExecuteNonQuery()
            conn1.Close()


        Next
        MessageBox.Show("Rows updated successfully")


    End Sub

    
   

   
   
    Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
        If e.KeyCode = Keys.Right Then


            For i As Integer = 0 To DataGridView1.Rows.Count - 1
                Dim constring5 As String = "Data Source=.;Initial Catalog=users;Integrated Security = true"
                Dim Query5 As String = "select id,name,country,id_group from table2 where id= '" & DataGridView1.Rows(i).Cells(0).Value & "'"
                Dim cn5 As SqlConnection = New SqlConnection(constring5)
                Dim cmd5 As SqlCommand = New SqlCommand(Query5, cn5)


                cn5.Open()

                If True Then

                    Using read5 As SqlDataReader = cmd5.ExecuteReader()

                        While read5.Read()
                            DataGridView1.Rows(i).Cells(1).Value = (read5("Name"))
                            DataGridView1.Rows(i).Cells(2).Value = (read5("Country"))



                        End While
                    End Using
                End If



                cn5.Close()





            Next
        End If
    End Sub
End Class