Results 1 to 3 of 3

Thread: How to edit/update some selected rows from datagridview to database through VB.NET?

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2019
    Posts
    54

    How to edit/update some selected rows from datagridview to database through VB.NET?

    I have a datagridview and loaded with records from a browsed database and combobox with items (different colors). The datagridview has 4 columns, the ID, FullName, Teamname, and last is the teamcolor. I want to update or edit the text in "teamcolor" column for each selected rows in datagridview. The code below can change the value of teamcolor in datagridview interface but it did not update actually in the database.

    Below is my constructed code:
    Code:
    Dim ObjConnection As New OleDbConnection()
           Dim i As Integer
    
            viewrecordsfrombackup.DataGridView1.SelectedRows(i).Cells("teamcolor").Value = ComboBox1.Text
           ObjConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & browserec.txtpath.Text & "'"
           Dim ObjCommand As New OleDbCommand()
           ObjCommand.Connection = ObjConnection
           For i = viewrecordsfrombackup.DataGridView1.SelectedRows.Count - 1 To 0 Step -1
    
               ObjCommand.CommandText = "UPDATE table1 SET teamcolor = '" & viewrecordsfrombackup.DataGridView1.SelectedRows(i).Cells("teamcolor").Value & "' where ID like '" & viewrecordsfrombackup.DataGridView1.SelectedRows(i).Cells("ID").Value & " '"
               ObjConnection.Open()
               ObjCommand.ExecuteNonQuery()
               ObjConnection.Close()
    
               Next
    MsgBox("Record(s) updated successfully!")

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to edit/update some selected rows from datagridview to database through VB.NE

    Follow the Codebank link in my signature below and check out my thread on Retrieving & Saving Data for an example of using a data adapter, with or without a command builder, to retrieve data and save changes. In between, you should bind your DataTable to a BindingSource and then bind that to the grid. When you're ready to save, call Validate on the form and EndEdit on the BindingSource before calling Update on the data adapter.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: How to edit/update some selected rows from datagridview to database through VB.NE

    Quote Originally Posted by ronelpisan View Post
    I have a datagridview and loaded with records from a browsed database and combobox with items (different colors). The datagridview has 4 columns, the ID, FullName, Teamname, and last is the teamcolor. I want to update or edit the text in "teamcolor" column for each selected rows in datagridview. The code below can change the value of teamcolor in datagridview interface but it did not update actually in the database.

    Below is my constructed code:
    Code:
    Dim ObjConnection As New OleDbConnection()
           Dim i As Integer
    
            viewrecordsfrombackup.DataGridView1.SelectedRows(i).Cells("teamcolor").Value = ComboBox1.Text
           ObjConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & browserec.txtpath.Text & "'"
           Dim ObjCommand As New OleDbCommand()
           ObjCommand.Connection = ObjConnection
           For i = viewrecordsfrombackup.DataGridView1.SelectedRows.Count - 1 To 0 Step -1
    
               ObjCommand.CommandText = "UPDATE table1 SET teamcolor = '" & viewrecordsfrombackup.DataGridView1.SelectedRows(i).Cells("teamcolor").Value & "' where ID like '" & viewrecordsfrombackup.DataGridView1.SelectedRows(i).Cells("ID").Value & " '"
               ObjConnection.Open()
               ObjCommand.ExecuteNonQuery()
               ObjConnection.Close()
    
               Next
    MsgBox("Record(s) updated successfully!")
    I don't know if this is the cause, but check your code ....
    Code:
     ObjCommand.CommandText = "UPDATE table1 SET teamcolor = '" & viewrecordsfrombackup.DataGridView1.SelectedRows(i).Cells("teamcolor").Value & "' where ID like '" & viewrecordsfrombackup.DataGridView1.SelectedRows(i).Cells("ID").Value & " '"
               ObjConnection.Open()
               ObjCommand.ExecuteNonQuery()
               ObjConnection.Close() '<------- here you close
    
               Next '<----- here you have Next, but you closed the connection
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width