datagridview列复制到另一个datagridview列,没有重复

  • 本文关键字:datagridview 另一个 复制 vb.net
  • 更新时间 :
  • 英文 :


我如何复制/克隆一个列从1 DataGrid视图到另一个DataGrid视图没有重复?有办法做到这一点吗?到目前为止,我还没有在谷歌上找到任何关于这个问题的解决方案……如有任何帮助,我将不胜感激。

我目前正在使用vb.net在VisualStudio2019中编码

例子
DGV1             |           DGV2 
Fruit       Qtt |          Fruit        Qtt       
Apple          2 |          Apple         4
Pear           3 |          Pear          6
Wintermelon    1 |          Wintermelon   1
Apple          2 |                    
Pear           3 |

DGV1列1移动到DGV2列1,没有重复/唯一我该怎么做呢?

' Iterate through the rows in the source GridView (GridView1)
For Each row As GridViewRow In GridView1.Rows
Dim data As String = row.Cells(0).Text ' Get the data from the first cell in the row
Dim exists As Boolean = False ' Flag to track whether the data already exists in the destination GridView

' Search for the data in the destination GridView's data source
For Each item As Object In GridView2.DataSource
If item.ToString() = data Then
exists = True
Exit For
End If
Next

' If the data does not exist in the destination GridView, add it
If Not exists Then
Dim dataSource As List(Of String) = TryCast(GridView2.DataSource, List(Of String))
dataSource.Add(data)
End If
Next
' Bind the updated data source to the destination GridView
GridView2.DataSource = dataSource
GridView2.DataBind()

最新更新