[英]How to set value of the current row based on data from the next row in a datagridview using vb.net
[英]if row has value in datagridview, insert data in the next row
我有兩個datagridview,在dgv1中我有一個刪除所選行的按鈕並將所選行傳輸到我的第二個dgv2,問題是,在第二次刪除一行時,它會覆蓋我刪除的行
我嘗試了多個代碼但它最終會遇到同樣的問題
Dim row As New DataGridViewRow
Dim nextrow As New DataGridViewRow
For Each row In DataGridView3.Rows
For Each nextrow In DataGridView3.Rows
If row.Index <> nextrow.Index Then
If row.Cells(0).Value = nextrow.Cells(0).Value AndAlso
row.Cells(2).Value = nextrow.Cells(2).Value AndAlso row.Cells(3).Value
= nextrow.Cells(3).Value AndAlso row.Cells(8).Value =
nextrow.Cells(8).Value Then
DataGridView3.Rows.Remove(row)
DataGridView3.Rows.Remove(nextrow)
End If
End If
Next
Next
它在第一次嘗試工作,但在第二次它覆蓋我的dgv2數據我想從我的dgv1顯示我刪除的數據到dgv2
當你使用集合並且你正在刪除項目時,一個好的技巧是向后走過集合而不是前進。 這是因為如果刪除/刪除集合中的一個項目,它可以更改集合。 例如,您刪除了第4項,因此第5項變為第4項,然后您嘗試使用第5項執行某些操作,但它現在是項目6等。
要消除此問題,請嘗試以下方法:
Dim myrows As Integer = gvw.rows.count
Dim row as GridViewRow, rownext as GridViewRow
For myrow As Integer = myrows -1 To 0 Step -1
row = gvw.rows(myrow)
rownext gvw.rows(myrow - 1)
'Do some comparison
If .. Then
gvw.rows.Remove(myrow) 'you can remove it because it is the last row
End If
If myrow = 1 Then Exit For 'cannot go lower
Next
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.