2017-04-24 67 views
0

我有datagridviews都显示数据(每个显示组 - 1和2),基本上他们都显示部分和插槽号码和一些其他位,部件号和插槽是主要信息,第一个gridview显示当前的零件和一个槽号,以及第二个gridview显示他们将要移动到的下一个零件(零件号和零件槽)。这些组之间有共同点,所以在第一个和第二个gridview中显示了插槽1中的一个示例1111-1111 - 我需要尝试的做法是对类似行的第二个gridview背景着色,如果它存在于第一个,但如果它不存在,则将该行着色为红色。比较Datagridviews vb.net

我已经得到了所有正确的数据显示,只需要在第一组红色中出现的第二个gridview上着色绿色和缺失的相似物。

我试过谷歌但nothings真的工作,我希望你们一个女孩可以帮助。

下面是填充datagridviews的大部分代码。

connect() 

    If combo_line.Text = "All" Then 
     cmd.CommandText = "SELECT v_machine, v_part, v_feedertype, v_slot, v_track FROM [sql_valor_groups] ORDER BY v_machine ASC" 
    Else 
     If combo_machine.Text = "All" Then 
      cmd.CommandText = "SELECT v_machine, v_part, v_feedertype, v_slot, v_track FROM [sql_valor_groups] WHERE v_line = '" & combo_line.Text & "' AND v_group = '" & combo_group.Text & "' ORDER BY v_machine ASC" 
      cmd2.CommandText = "SELECT v_machine, v_part, v_feedertype, v_slot, v_track FROM [sql_valor_groups] WHERE v_line = '" & combo_line.Text & "' AND v_group = '" & combo_group2.Text & "' ORDER BY v_machine ASC" 
     Else 
      cmd.CommandText = "SELECT v_machine, v_part, v_feedertype, v_slot, v_track FROM [sql_valor_groups] WHERE v_line = '" & combo_line.Text & "' AND v_machine = '" & combo_machine.Text & "' AND v_group = '" & combo_group.Text & "' ORDER BY v_machine ASC" 
      cmd2.CommandText = "SELECT v_machine, v_part, v_feedertype, v_slot, v_track FROM [sql_valor_groups] WHERE v_line = '" & combo_line.Text & "' AND v_machine = '" & combo_machine.Text & "' AND v_group = '" & combo_group2.Text & "' ORDER BY v_machine ASC" 
     End If 

    End If 


    Dim dataAdapter = New SqlDataAdapter(cmd.CommandText, con.ConnectionString) 
    Dim table As New DataTable() 
    table.Locale = System.Globalization.CultureInfo.InvariantCulture 
    dataAdapter.Fill(table) 
    Me.BindingSource.DataSource = table 

    DataGridView.DataSource = BindingSource 

    DataGridView.Columns(0).HeaderText = "Machine:" 
    DataGridView.Columns(1).HeaderText = "Part:" 
    DataGridView.Columns(2).HeaderText = "Feeder Type:" 
    DataGridView.Columns(3).HeaderText = "Slot:" 
    DataGridView.Columns(4).HeaderText = "Track:" 

    DataGridView.EnableHeadersVisualStyles = False 

    DataGridView.RowTemplate.MinimumHeight = 30 



    Dim dataAdapter2 = New SqlDataAdapter(cmd2.CommandText, con2.ConnectionString) 
    Dim table2 As New DataTable() 
    table2.Locale = System.Globalization.CultureInfo.InvariantCulture 
    dataAdapter2.Fill(table2) 
    Me.BindingSource2.DataSource = table2 

    DataGridView2.DataSource = BindingSource2 

    DataGridView2.Columns(0).HeaderText = "Machine:" 
    DataGridView2.Columns(1).HeaderText = "Part:" 
    DataGridView2.Columns(2).HeaderText = "Feeder Type:" 
    DataGridView2.Columns(3).HeaderText = "Slot:" 
    DataGridView2.Columns(4).HeaderText = "Track:" 

    DataGridView2.EnableHeadersVisualStyles = False 

    DataGridView2.RowTemplate.MinimumHeight = 30 

    disconnect() 

回答

1

迭代中datagridview2

For i As Integer = 0 To DataGridView.Rows.Count() - 1 Step +1 
     Dim row As DataGridViewRow = DataGridView.Rows(i) 
     If DataGridView2.Rows.Count() > 0 Then 
      For j As Integer = 0 To DataGridView2.Rows.Count() - 1 Step +1 
       If row.Cells(1).Value.ToString() = DataGridView2.Rows(j).Cells(1).Value.ToString() And row.Cells(3).Value.ToString() = DataGridView2.Rows(j).Cells(3).Value.ToString() Then 
        DataGridView2.Rows(i).DefaultCellStyle.BackColor = Color.Green 
       Else 
        DataGridView2.Rows(i).DefaultCellStyle.BackColor = Color.Red 
       End If 
      Next 
     End If 
    Next 
从DataGridView和匹配您行