从Datagridview中查找常用值

时间:2013-11-15 11:04:46

标签: vb.net

我有大约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

1 个答案:

答案 0 :(得分: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

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