Click here to Skip to main content
15,887,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

VB
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!")


What I have tried:

VB
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!")
Posted
Updated 14-Feb-21 6:55am
Comments

1 solution

I have done it in Vb.net and MySql. I have passed all the data from grid to stored procedure through array parameter and easily, i could process the data in the server. It is faster, based on my experience.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900