如何将一行复制到gridview中的另一行 [英] How to copy One row to another row in gridview
问题描述
使用VB.Net
我想将一行数据复制到另一行。
I want to Copy one row data to another row.
我是在gridview中使用复选框,如果我点击复选框,然后按按钮,然后选择行复制到新单元格(行)
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
代码
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控件。
The above code is showing error as "Row provided already belongs to a DataGridView control."
我的代码有什么问题。
需要VB.Net代码帮助
Need VB.Net Code Help
你不能再次添加完全相同的行。您将需要创建一个新行,并使用重复的行中的值进行填充,然后将新行添加到grvList.Rows
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
保留请注意,如果任何单元格中的项目是仍将引用相同项目的引用类型,而不是创建项目的副本。很简单,只需在同一行中调用add即可。
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.
对不起,我错过了这些行是DataGridView行,而不是绑定的datatable。在这种情况下,这应该是诀窍:
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
这篇关于如何将一行复制到gridview中的另一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!