如何在VB.NET中将datagridview中的多个选定行更新为另一个 [英] How do I update multiple selected row in datagridview to another in VB.NET

查看:86
本文介绍了如何在VB.NET中将datagridview中的多个选定行更新为另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个datagridview:dgServer和dgBranch,我在选择行时使用复选框,我想从dgServer中的选定行更新dgbranch的数据我选择了多行,但最后选择的行是唯一更新的。



我尝试了什么:



我有两个datagridview: dgServer和dgBranch,

i我在选择行时使用复选框,我想

从dgServer中的选定行更新dgbranch的数据。

我选择了多行,但最后选择的行是唯一更新的。



i已尝试此代码,但此代码仅更新一行



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

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

推荐答案

您只获得最后一行,因为这是您的所有代码更新,CurrentRow是所选的最后一行。



例如,使用For Each循环来遍历所选行;



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





如果使用选中的行,则可能需要遍历整行行检查checked = true,例如,请参阅

[ ^ ]



也永远不要连接字符串来构造SQL语句,总是使用参数化查询。



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.


这篇关于如何在VB.NET中将datagridview中的多个选定行更新为另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆