Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two datagridview: dgServer and dgBranch, i am using checkbox in selecting the row and i want to update the data of the dgbranch from the selected row in dgServer i have selected multiple rows but the last selected row is the only updated.

What I have tried:

I have two datagridview: dgServer and dgBranch,
i am using checkbox in selecting the row and i want to
update the data of the dgbranch from the selected row in dgServer.
i have selected multiple rows but the last selected row is the only updated.

i have tried this code but this code only update a single row

VB
Private Sub btnUpdateS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdateS.Click

        Try
            Dim dr As New DialogResult
            dr = MessageBox.Show("Are you sure you want to Update?", "Transfer", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
            If dr = Windows.Forms.DialogResult.OK Then
                If conn1.State = ConnectionState.Closed Then
                    conn1.ConnectionString = "Server= localhost;Database=jlt_2;username=root;password=SystemDeveloper2011;"
                    conn1.Open()
                End If
                Dim st As String = "UPDATE prcpricing SET deal_price = '" & DataGridView1.CurrentRow.Cells(3).Value & _
                                        "', unit_price = '" & DataGridView1.CurrentRow.Cells(4).Value & "', unit_amt = '" & DataGridView1.CurrentRow.Cells(5).Value & _
                                        "'  WHERE p_code = '" & DataGridView1.CurrentRow.Cells(1).Value & "' AND b_code = '" & dgBranch.CurrentRow.Cells(2).Value & "'"
                MessageBox.Show((DataGridView1.CurrentRow.Cells(1).Value) & (DataGridView1.CurrentRow.Cells(2).Value) & (DataGridView1.CurrentRow.Cells(3).Value) & (DataGridView1.CurrentRow.Cells(4).Value) & (DataGridView1.CurrentRow.Cells(5).Value))
                Dim cmd As New MySqlCommand(st, conn1)
                cmd.ExecuteNonQuery()
                MessageBox.Show("Process Successful!", "Transfer", MessageBoxButtons.OK, MessageBoxIcon.Information)

                conn1.Close()
            End If
        Catch err As Exception
            MessageBox.Show(err.ToString)
        End Try
      
    End Sub
Posted
Updated 30-Jun-17 19:50pm
v3
Comments

1 solution

You only get the last row because that is all your code updates, CurrentRow is the last row selected.

Use a For Each loop to walk the selected rows for example;

For Each dgr as DataGridViewRow in DataGridView1.SelectedRows
Dim st As String = "UPDATE prcpricing SET deal_price = '" & dgr.Cells(3).Value & _

...etc.
Next


If you are using checked rows you may have to walk the entire set of rows checking for checked = true, for an example see
[^]

Also never concatenate a string to construct an SQL statement, always use parameterised queries.
 
Share this answer
 
v2
Comments
Member 12374765 5-Jul-17 23:27pm    
Hi!
Thank you for the solution :)

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