我想比较两个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
因此,我尝试将数据 fromdgv1放入一个数组中,并将数据 fromdgv2放入一个动态数组中,因为我想要每个列表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]",当我尝试尝试 castexceptItems时,如下所示:
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,有没有什么方法可以做到这一点,而无需对我的代码进行重大更改?