比较Datagridviews vb.net

时间:2017-04-24 09:57:12

标签: sql sql-server vb.net datagridview

我有datagridviews同时显示数据(每个显示一组-1和2),基本上它们都显示部件和插槽号以及一些其他位,部件号和插槽是主要信息,第一个gridview显示他们去的当前部分和一个插槽号,第二个gridview显示他们将要移动到的下一个组(部件号和插槽)。组之间存在共性,因此在第一和第二个网格视图中显示了插槽1中的示例1111-1111 - 我需要尝试做的是将类似行的第二个网格视图背景着色为绿色(如果它存在于第一个,但如果它不存在,则将该行涂成红色。

我已经显示了所有正确的数据,只需要在第一组红色的第二个网格视图中为绿色和缺失的相似性着色。

我已经尝试了谷歌,但没有真正的工作,我希望你们一个女孩可以帮助。

以下是填充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 个答案:

答案 0 :(得分:1)

从datagridview迭代行并在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