我想比較兩個datagridviews,並在IEnumerable
接口上使用Except
方法,以瞭解它們之間的區別。我datagridviews的 一個例子:將一個數組對象轉換爲IEnumerable
DG1 idProduct Item 1 Item A 1 Item B 2 Item C 2 Item D DG2 idProduct Item Price IdSupplier 1 Item A 10.00 1 1 Item B 20.00 1 2 Item C 30.00 1 2 Item D 40.00 1 1 Item A 20.00 3 1 Item B 30.00 3 2 Item C 40.00 3 2 Item 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
,但似乎我的itemArray()
是一個對象,因爲我得到的消息"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
,有什麼方法可以在不對代碼進行重大更改的情況下執行此操作?
冬青之神!謝謝!我打開了這個東西,看了很多工作......我會再次關閉它...... – user2554904