【问题标题】:Find duplicats and display in new datagridview查找重复项并在新的 datagridview 中显示
【发布时间】:2014-09-28 10:24:45
【问题描述】:

我有两个数据网格视图:

DataGridView1: 这是使用以下代码从 csv 文件生成的:

For Each line As String In System.IO.File.ReadAllLines("C:\path\test.csv")
                Form1.DataGriView1.Rows.Add(line.Split(";"))
 Next

.csv 文件具有以下格式:

12345;一些文字;2000000;12345678901;2014-07-31;

23456;一些文字;2000000;10987654321;2014-07-11;

DataGridView2 这是使用以下代码从 excel 文件生成的:

 Dim MyConnection As System.Data.OleDb.OleDbConnection
            Dim DtSet As System.Data.DataSet
            Dim MyCommand As System.Data.OleDb.OleDbDataAdapter

            MyConnection = New System.Data.OleDb.OleDbConnection _
                ("provider=Microsoft.Jet.OLEDB.4.0; Data Source='C:\Users\path\excel.xls'; Extended Properties=Excel 8.0;")
            MyCommand = New System.Data.OleDb.OleDbDataAdapter _
                    ("select * from [Sheet$]", MyConnection)
            DtSet = New System.Data.DataSet
            MyCommand.Fill(DtSet)
            Form1.DataGridView2.DataSource = DtSet.Tables(0)
            MyConnection.Close()

Excel 文档有 6 列,带有标题。

问题:

我需要查找重复项并在第三个网格视图中显示数据。具体来说,我想看看DataGridView1中column1中的值是否在DataGridView2中的polnr列中。如果是这样,我希望将 DataGridView 的整行复制到 DataGridView3

是的,我自己尝试过并搜索了整个互联网。有人可以帮我吗?

【问题讨论】:

    标签: vb.net datagridview


    【解决方案1】:

    这应该适用于未绑定的 dgv 1 + 3

        Dim rowlist As New ArrayList
        Dim dgv3row As New DataGridViewRow
    
        For Each dgv1row As DataGridViewRow In DataGridView1.Rows
            For Each dgv2row As DataGridViewRow In DataGridView2.Rows
    
                If dgv1row.Cells("Column1").Value = dgv2row.Cells("polnr").Value Then
    
                    For Each dgvcell As DataGridViewCell In dgv1row.Cells
                        rowlist.Add(dgvcell.Value)
                    Next
    
                    If rowlist.Count > 0 Then
                        Dim dgv3rowindex As Integer = DataGridView3.Rows.Add()
    
                        dgv3row = DataGridView3.Rows(dgv3rowindex)
    
                        For Each dgv3cell As DataGridViewCell In dgv3row.Cells
                            dgv3cell.Value = rowlist(dgv3cell.ColumnIndex)
                        Next
    
                        rowlist.Clear()
                    End If
                End If
            Next
        Next
    

    【讨论】:

      猜你喜欢
      • 2015-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 2012-05-04
      相关资源
      最近更新 更多