Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 使用Linq查找副本并在新datagridview中显示_Vb.net_Excel_Csv_Datagridview - Fatal编程技术网

Vb.net 使用Linq查找副本并在新datagridview中显示

Vb.net 使用Linq查找副本并在新datagridview中显示,vb.net,excel,csv,datagridview,Vb.net,Excel,Csv,Datagridview,我有两个DataGridView: DataGridView1: 这是使用以下代码从csv文件生成的: For Each line As String In System.IO.File.ReadAllLines("C:\path\test.csv") Form1.DataGriView1.Rows.Add(line.Split(";")) Next Dim MyConnection As System.Data.OleDb.OleDbConnection

我有两个DataGridView:

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

For Each line As String In System.IO.File.ReadAllLines("C:\path\test.csv")
                Form1.DataGriView1.Rows.Add(line.Split(";"))
 Next
 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()
.csv文件具有以下格式:

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

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

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

For Each line As String In System.IO.File.ReadAllLines("C:\path\test.csv")
                Form1.DataGriView1.Rows.Add(line.Split(";"))
 Next
 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列,带有标题

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

我今天正在使用这个代码,但问题是它太慢了

将行列表变暗为新的ArrayList 将dgv3row调整为新的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

我的问题是:这是一种更快的方法吗?像LINQ之类的?

您可以使用LINQ查询重复记录:

Dim dupRecords = (From data1 As DataGridViewRow In DataGridView1.Rows
                          Join data2 As DataGridViewRow In DataGridView2.Rows
                          On data1.Cells("Column1").Value Equals data2.Cells("polnr").Value
                          Select data2).ToList
然后遍历重复的行结果并添加到第三个datagridview。以下假设列已在第三个datagridview中创建:

Dim rowIndex As Integer = 0

For Each row In dupRecords
   For cellIndex As Integer = 0 To row.Cells.Count - 1
     DataGridView3.Rows(rowIndex).Cells(cellIndex).Value = _
        row.Cells(cellIndex).Value
   Next cellIndex
   rowIndex += 1
Next row

当您可以直接将信息添加到dgv3row跳过行列表时,为什么要先将信息添加到行列表中,然后添加到dgv3row中?