gridview中如何将一行复制到另一行
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法再次添加完全相同的行。您将需要创建一个新行并用您要复制的行中的值填充它,然后将新行添加到 grvList.Rows
我不确定每个单元格中拥有什么类型的值,但只要它们是值类型,如下所示应该可以工作:
请记住,如果任何单元格中的项目是引用类型,您仍将引用同一项目,而不是创建该项目的副本。就像您通过简单地在您找到的同一行上调用 add 所做的那样。
抱歉,我错过了这些行是 DataGridView 行,而不是绑定的数据表...在这种情况下这应该可以解决问题:
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:
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: