Compare two DataGridViews in C #

0

Hello World !, I have a small detail, from a button I am comparing two columns of two datagridviews. What I want to get is: From the DataDridView1 to get all the data that does not exist in DataGridView2 . What I really did is compare the data and those that are the same paint the row of DataGridView1 , until there all good.

My problem is in the same DataGridView1 I have a checkbox in the first column ( Cells(0) ) and I have to activate the checkboxes for the data that do not match. According to me if the CheckBox is deactivated and when NOT, they activate it but that logic does not work for me because it leaves all of them activated.

Annex code to see where I'm doing wrong.

private void btnAdicionales_Click(System.Object sender, System.EventArgs e)
{
    int intContador = 0;
    for (var i = 0; i <= this.DataGridView1.Rows.Count - 1; i++)
    {
        for (var j = 0; j <= this.DataGridView2.Rows.Count - 1; j++)
        {
            if (Trim(System.Convert.ToString(DataGridView1.Rows(i).Cells(1).Value)) == Trim(System.Convert.ToString(DataGridView2.Rows(j).Cells(0).Value)))
            {
                DataGridView1.Rows(i).Cells(1).Style.BackColor = Color.Coral;
                DataGridView1.Rows(i).Cells(2).Style.BackColor = Color.Coral;
                DataGridView1.Rows(i).Cells(3).Style.BackColor = Color.Coral;
                DataGridView1.Rows(i).Cells(4).Style.BackColor = Color.Coral;
                DataGridView1.Rows(i).Cells(5).Style.BackColor = Color.Coral;
                DataGridView1.Rows(i).Cells(7).Style.BackColor = Color.Coral;
                intContador += 1;

                DataGridView1.Rows(i).Cells(0).Value = false;
            }
            else
                DataGridView1.Rows(i).Cells(0).Value = true;
        }
    }
    int intDif = 0;
    intDif = intContador - System.Convert.ToInt32(DataGridView1.Rows.Count);
    ToolStripStatusLabel3.Text = "Adicionales: " + System.Convert.ToString(intDif);
}

I appreciate the suggestions you can give me.

    
asked by SdeSistemas 06.10.2018 в 19:48
source

1 answer

1

What you can do is a procedure that activates all the checkBox you have in your DataGridView, something like this:

public void activaChecks()
{
    foreach (DataGridViewRow row in DataGridView1.dgvExistencia.Rows){
        row.Cells(0).Value = true;
    }
}

already in your button, this procedure instances before making your comparisons and you are only deactivating the checkBox that contains the matching data, with that it is no longer necessary to use your ELSE

You can also go "breaking" your cycle for so that it does not go through all DataGridView2 , if you find a matching data, you can exit and continue with the next value.

Your final code might look something like this:

private void btnAdicionales_Click(System.Object sender, System.EventArgs e)
{
    activaChecks();
    int intContador = 0;
    for (var i = 0; i <= this.DataGridView1.Rows.Count - 1; i++)
    {
        for (var j = 0; j <= this.DataGridView2.Rows.Count - 1; j++)
        {
            if (Trim(System.Convert.ToString(DataGridView1.Rows(i).Cells(1).Value)) == Trim(System.Convert.ToString(DataGridView2.Rows(j).Cells(0).Value)))
            {
                DataGridView1.Rows(i).Cells(1).Style.BackColor = Color.Coral;
                ...
                DataGridView1.Rows(i).Cells(7).Style.BackColor = Color.Coral;
                intContador += 1;
                DataGridView1.Rows(i).Cells(0).Value = false;
                break; // romper el ciclo.'
            }
        }
    }
    ....
}
    
answered by 08.10.2018 / 21:34
source