gridview中如何将一行复制到另一行

发布于 2024-12-09 17:55:19 字数 628 浏览 0 评论 0原文

使用 VB.Net

我想将一行数据复制到另一行。

我在 gridview 中使用复选框,如果我单击复选框并按下按钮,然后选择行复制到新单元格(行),

下面的代码适用于删除,不适用于复制行

代码

 Private Sub btncopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncopy.Click
        For Each m_row As System.Windows.Forms.DataGridViewRow In Me.grvList.Rows
            If m_row.Cells("chksel").Value = True Then
               Me.grvList.Rows.Add(m_row)
             '  Me.grvList.Rows.Remove(m_row)
            End If
        Next
    End Sub

上面的代码显示错误为“提供的行”已经属于 DataGridView 控件。”

我的代码有什么问题。

需要 VB.Net 代码帮助

Using VB.Net

I want to Copy one row data to another row.

I am using checkbox in the gridview, if i clicked the checkbox and press button then selected row copy to new cell (row)

Below code is working for delete, not working for copy the rows

Code

 Private Sub btncopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncopy.Click
        For Each m_row As System.Windows.Forms.DataGridViewRow In Me.grvList.Rows
            If m_row.Cells("chksel").Value = True Then
               Me.grvList.Rows.Add(m_row)
             '  Me.grvList.Rows.Remove(m_row)
            End If
        Next
    End Sub

The above code is showing error as "Row provided already belongs to a DataGridView control."

What wrong with my code.

Need VB.Net Code Help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静水深流 2024-12-16 17:55:19

您无法再次添加完全相同的行。您将需要创建一个新行并用您要复制的行中的值填充它,然后将新行添加到 grvList.Rows

我不确定每个单元格中拥有什么类型的值,但只要它们是值类型,如下所示应该可以工作:

    For Each m_row As System.Windows.Forms.DataGridViewRow In Me.grvList.Rows
        If m_row.Cells("chksel").Value = True Then
            'Create new row to hold duplicated values
            Dim NewRow As DataRow = grvList.NewRow()
            'loop thru existing rows to copy values
            For i As Integer = 0 To m_row.Cells.Count - 1
                NewRow(i) = m_row.Cells(i).Value
            Next
            'Add newly create row to table
            Me.grvList.Rows.Add(NewRow)
            '  Me.grvList.Rows.Remove(m_row)
        End If
    Next

请记住,如果任何单元格中的项目是引用类型,您仍将引用同一项目,而不是创建该项目的副本。就像您通过简单地在您找到的同一行上调用 add 所做的那样。

抱歉,我错过了这些行是 DataGridView 行,而不是绑定的数据表...在这种情况下这应该可以解决问题:

            For Each m_row As System.Windows.Forms.DataGridViewRow In Me.grvList.Rows
                If m_row.Cells("chksel").Value = True Then
                    'Create new row to hold duplicated values
                     Dim NewRow As DataGridViewRow = m_row.Clone
                     'Add newly create row to table
                     Me.grvLIst.Rows.Add(NewRow)
                End If
            Next

You can't add exactly the same row again. You will need to create a new row and populate it with the values from the row you are duplicating instead, then add the new row to the grvList.Rows

I am not sure what sorts of values you have in each cell, but as long as they are value types something like the following should work:

    For Each m_row As System.Windows.Forms.DataGridViewRow In Me.grvList.Rows
        If m_row.Cells("chksel").Value = True Then
            'Create new row to hold duplicated values
            Dim NewRow As DataRow = grvList.NewRow()
            'loop thru existing rows to copy values
            For i As Integer = 0 To m_row.Cells.Count - 1
                NewRow(i) = m_row.Cells(i).Value
            Next
            'Add newly create row to table
            Me.grvList.Rows.Add(NewRow)
            '  Me.grvList.Rows.Remove(m_row)
        End If
    Next

Keep in mind that if the items in any of the cells are reference types that you will still be referencing the same item, instead of creating a copy of the item. Much as you were doing by simply calling add on the same row you located.

Sorry, I missed that the rows were DataGridView rows, rather than a datatable that was bound... this should do the trick in that case:

            For Each m_row As System.Windows.Forms.DataGridViewRow In Me.grvList.Rows
                If m_row.Cells("chksel").Value = True Then
                    'Create new row to hold duplicated values
                     Dim NewRow As DataGridViewRow = m_row.Clone
                     'Add newly create row to table
                     Me.grvLIst.Rows.Add(NewRow)
                End If
            Next
高跟鞋的旋律 2024-12-16 17:55:19
'To copy Row
Private Sub CopyButton_Click(sender As System.Object, e As System.EventArgs) Handles CopyButton.Click
    CopyRowIndex = DGV1.CurrentRow.Index
End Sub

'To Paste Row
Private Sub PasteButton_Click(sender As System.Object, e As System.EventArgs) Handles PasteButton.Click
    PasteRowIndex = DGV1.CurrentRow.Index
    For index As Int32 = 0 To DGV1.ColumnCount - 1
        DGV1.Rows(CInt(PasteRowIndex)).Cells(index).Value = DGV1.Rows(CInt(CopyRowIndex)).Cells(index).Value
    Next

End Sub

'To Duplicate Rows
Private Sub DuplicateButton_Click(sender As System.Object, e As System.EventArgs) Handles DuplicateButton.Click
    CopyRowIndex = DGV1.CurrentRow.Index
    DGV1.Rows.Add()
    DuplicateRowIndex = DGV1.Rows.Count - 1
    For index As Int32 = 0 To DGV1.ColumnCount - 1
        DGV1.Rows(CInt(DuplicateRowIndex)).Cells(index).Value = DGV1.Rows(CInt(CopyRowIndex)).Cells(index).Value
    Next
End Sub
'To copy Row
Private Sub CopyButton_Click(sender As System.Object, e As System.EventArgs) Handles CopyButton.Click
    CopyRowIndex = DGV1.CurrentRow.Index
End Sub

'To Paste Row
Private Sub PasteButton_Click(sender As System.Object, e As System.EventArgs) Handles PasteButton.Click
    PasteRowIndex = DGV1.CurrentRow.Index
    For index As Int32 = 0 To DGV1.ColumnCount - 1
        DGV1.Rows(CInt(PasteRowIndex)).Cells(index).Value = DGV1.Rows(CInt(CopyRowIndex)).Cells(index).Value
    Next

End Sub

'To Duplicate Rows
Private Sub DuplicateButton_Click(sender As System.Object, e As System.EventArgs) Handles DuplicateButton.Click
    CopyRowIndex = DGV1.CurrentRow.Index
    DGV1.Rows.Add()
    DuplicateRowIndex = DGV1.Rows.Count - 1
    For index As Int32 = 0 To DGV1.ColumnCount - 1
        DGV1.Rows(CInt(DuplicateRowIndex)).Cells(index).Value = DGV1.Rows(CInt(CopyRowIndex)).Cells(index).Value
    Next
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文