2012-10-18 52 views
0

複製使用VB.Net如何在最後一排的DataGrid

我想最後一排的值複製到新行

代碼

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代碼幫助

回答

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。