【发布时间】:2013-07-04 23:28:03
【问题描述】:
我想比较两个datagridviews,并在IEnumerable接口上使用Except方法,以了解它们之间的区别。
我的数据网格视图的一个示例:
所以,我试图将来自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,但我的itemArray()似乎是一个对象,因为我在尝试投射exceptItems时收到消息"System.linq.Enumerable+<ExceptIterator>d_99'1[System.Object]",如下:
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,有什么方法可以在不对代码进行重大更改的情况下做到这一点?
【问题讨论】: