2013-11-15 89 views
0

我有大約10(DatagridView計數可能會根據用戶從2到10選定的文件而不同)Datagridview,那麼我如何從所有Datagridviews中找到共同的價值?從Datagridview查找公共值

評論如果您需要更多的簡短詳情

下面是我的,但它找到共同從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 
+0

分享您的代碼 – SK2185

+0

@SenthilKumar檢查現在 –

+0

是否使用多個datagridview的 – SK2185

回答

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 

你的代碼是不是太清楚(既不w ^你想要的帽子);但是這應該給你一個足夠好的開始來執行你正在尋找的實現。請記住,這個代碼(像你的)只考慮一列(第一個);如果要遍歷所有列,則必須添加進一步的嵌套循環。

+0

我意思是從所有Datagridviews中找到所有可能的常見指令 –

+0

@JadejaRJ這就是這段代碼所做的有限的事情(只查看第一列並只尋找每個值的一個匹配)。我希望你明白,這不是一個定製的軟件開發服務,而且這個答案遠遠超出了當前條件下可以接受的程度(問題集中在你的具體問題上,而不是你身邊的太多努力(你的代碼代表很差的嘗試))。請隨時提出任何問題,但我希望我的立場足夠清楚。 – varocarbas

+0

@JadejaRJ我知道爲什麼不標記/不標記問題?有什麼不清楚的嗎? – varocarbas