0

我有大约 10 个(DatagridView 计数可能会根据用户选择的文件从 2 到 10 变化)Datagridview,那么我怎样才能从所有 Datagridviews 中找到共同值?

评论 如果您需要更简短的详细信息

下面是我的,但它在 2 -2 datagridviews 中很常见

 For i As Integer = 1 To dgvCont
        For j As Integer = 0 To Main.DGVM(i).Rows.Count - 1
            For Each Val As DataGridViewRow In Main.DGVM(i + 1).Rows
                If Val.Cells(0).Value = Main.DGVM(i).Rows.Item(j).Cells(0).Value Then
                    Dim cm As String = Val.Cells(0).Value
                    If cm = "" Then

                    Else
                        Analysis.lvCmn.Items.Add(Val.Cells(0).Value)
                    End If
                End If
            Next
        Next
    Next
4

1 回答 1

1

我了解您想要设置两个嵌套循环来解释未确定数量的元素(DataGridView我认为数组中的项目),执行您想要的检查:

 For count1 As Integer = 1 To dgvCont 'Assuming indices from 1 to dgvCont

     For row1 As Integer = 0 To Main.DGVM(count1).Rows.Count - 1
         If (Main.DGVM(count1).Rows(row1).Cells(0).Value Is Nothing) Then Continue For

         Dim val1 As String = Main.DGVM(count1).Rows(row1).Cells(0).Value
         Dim found As Boolean = False
         For count2 As Integer = 1 To dgvCont 'Assuming indices from 1 to dgvCont
             If (count2 = count1) Then Continue For

             For row2 As Integer = 0 To Main.DGVM(count2).Rows.Count - 1
                 If (Main.DGVM(count2).Rows(row2).Cells(0).Value Is Nothing) Then Continue For

                 Dim val2 As String = Main.DGVM(count2).Rows(row2).Cells(0).Value.ToString()
                 If val1 = val2 Then
                    Dim cm As String = val1
                    If cm = "" Then

                    Else
                        Analysis.lvCmn.Items.Add(val1)
                    End If

                    found = True
                    Exit For 'By assuming that you want to stop searching after finding a match
                 End If

             Next

             If (found) Then Exit For 'By assuming that you want to stop searching after finding a match
         Next
     Next
 Next

你的代码不是太清楚(不是你想要的);但这应该给你一个足够好的开始来执行你正在寻找的实现。请记住,此代码(如您的代码)仅考虑一列(第一列);如果想要遍历所有列,则必须添加进一步的嵌套循环来解决此问题。

于 2013-11-15T12:26:56.717 回答