使用VB.Net
我想将最后一行值复制到新行
代码
Dim rcount As Int32
For i As Integer = 0 To gridview1.RowCount - 1
gridview1.Rows.Add()
rcount = gridview1.RowCount() - 1
For j As Integer = 0 To gridview1.Rows(i).Cells.Count - 1
gridview1.Rows(rcount).Cells(j).Value = gridview1.Rows(i).Cells(j).Value
Next
gridview1.Rows(rcount).Cells(1).Value = rcount + 1
rcount = 0
Next
上面的代码正在运行,但是它将所有前一行复制到新行,我想将最后一行复制到新行
如何解决这个问题。
需要VB.Net代码帮助
答案 0 :(得分:1)
如果您只想将最后一行复制到新行,请执行此操作。
Dim LastRow As Integer = DataGridView1.Rows.Count - 1
Dim Newrow As Integer = DataGridView1.Rows.Add()
For i As Integer = 0 To DataGridView1.Columns.Count - 1
DataGridView1.Rows(Newrow).Cells(i).Value = DataGridView1.Rows(LastRow).Cells(i).Value
Next
如果您始终存在空白的可编辑行,则此操作无效。然后你需要从“Dim LastRow”行中减去2。