0

2 つの datagridview を比較し、インターフェイスでExceptメソッドを使用して、それらの違いを知りたいです。IEnumerable私のデータグリッドビューの一例:

DG1

idProduct アイテム

1 アイテム A
1 アイテム B
2 アイテム C
2 アイテム D

DG2
idProduct 商品価格 IdSupplier
1 アイテム A 10.00 1
1 アイテム B 20.00 1
2 アイテム C 30.00 1
2 アイテム D 40.00 1
1 アイテム A 20.00 3
1 アイテム B 30.00 3
2 アイテム C 40.00 3
2 アイテム D 50.00 3

dgv1そのため、データを配列に入れ、データを動的配列に入れようとしましたdgv2。それぞれのリストIdSupplier(この場合は 1、3) が必要で、except メソッドと比較します。私のコードは次のとおりです。

Imports System.Data.SqlClient
Imports System.Data
Imports datagridviewTota.DataSet1TableAdapters
Imports System.Collections.Generic
Imports System.Collections.ArrayList
Imports System.Collections.CollectionBase

Public Class form1

Public itemArray()
Public ItemListArray()
Public Shared j As Integer

Private sub main ()

   (…)
   Dim ds1 As New DataSet
   Dim item As List(Of String) = New List(Of String)
   Dim Id As Integer
   Dim dr As DataRow
   Dim dr1 As DataRow
   Dim itemList As List(Of String) = New List(Of String)
   Dim idSuppliers () as integer
   ReDim itemArray(j) ‘ j represents the numbers of elements in idSuppliers() (third column of dg2)

 Try
    //create an array for the dgv1//
    For Each row As DataGridViewRow In dgv1.Rows                  
       item = dgv1.Rows(row.Index).Cells(1).Value
       itemList.Add(item)                                
    Next

    Dim itemListArray = itemList.toArray()

    //create a dynamic array for the dgv2 by filtering for each idSuppliers, put the values from the second column into a list and convert each list into a dynamic array//

    For Each element In idSuppliers
       Dim dv As New DataView()
       dv = New DataView(ds1.Tables("idProduct"))
            With dv
                .RowFilter = "idSupplier = " & element & " "
            End With
       dgv2.DataSource = dv

       For Each row As DataGridViewRow In dgv2.Rows                 
          Id = dgv2.Rows(row.Index).Cells(3).Value

          If Id = element Then
             item = dgv2.Rows(row.Index).Cells(1).Value
             itemList.Add(item)                         
          End If                            
       Next

       itemArray(i) = itemList.ToArray()
       itemList.clear()

       i = i + 1     
    Next
end sub

を試してみましたが、次のようにキャストしようとすると、メッセージが表示されIEnumerable.Exceptたため、 myitemArray()はオブジェクトのようです。"System.linq.Enumerable+<ExceptIterator>d_99'1[System.Object]"exceptItems

Dim exceptItems = itemListArray.Except(itemArray(2))

私も試しました:

Dim onlyInFirstSet As IEnumerable(Of String) = itemListArray.Except(itemArray(2))

            Dim output As New System.Text.StringBuilder
            For Each str As String In onlyInFirstSet
               output.AppendLine(str)
            Next
            MsgBox(output.ToString())

そして、私は間違いを犯すことを知っています。番号 13. 問題は、に変換する必要があることだと思いitemArray()ますIEnumerable。コードを大幅に変更せずにこれを行う方法はありますか?

4

2 に答える 2