compare datatable with gridview

0

My question is as follows: I have a gridview that I populate it with a data table of a table called "DiscountDate", however I need a button that when clicked it will go into another table named "sectors" and go through each row of the sectors table and then check if the sector found in the "sectors" table is already in the datagrid.

I've tried this, but I'm not experiencing success:

private void IncluiTodosSetores_Click(object sender, EventArgs e)
{
    DataTable tabela = Dados.carregaSetoresTodos();
    foreach (DataRow linha in tabela.Rows)
    {
        int achou = 0;
        foreach (DataGridViewRow dataGridViewRow in GridDesconto.Rows)
        {
            int codorig = Convert.ToInt16(linha["Codigo"]);
            int codgrid = Convert.ToInt16(dataGridViewRow.Cells["setor_codigo"].Value);
            if (codorig.ToString() == codgrid.ToString())
            {
                achou = 1;
                break;
            }
            else
            {
                achou = 0;
                break;
            }
        }

        if (achou == 0)
        {
            //inclui o setor nao encointrado no grid
        }
    }
}
    
asked by anonymous 21.11.2017 / 15:23

1 answer

0

Resolved !!!!

It was a BREACK too damn ...

I just wanted to comment on what worked ...

As I solved it myself, what is the procedure to close this topic?

Thanks to all ...

private void IncluiTodosSetores_Click(object sender, EventArgs e)
{
    DataTable tabela = Dados.carregaSetoresTodos();
    foreach (DataRow linha in tabela.Rows)
    {

        //MessageBox.Show(linha["Codigo"].ToString());

        int achou = 0;
        foreach (DataGridViewRow dataGridViewRow in GridDesconto.Rows)
        {
            //MessageBox.Show(dataGridViewRow.Cells["setor_codigo"].Value.ToString());

            int codorig = Convert.ToInt16(linha["Codigo"]);
            int codgrid = Convert.ToInt16(dataGridViewRow.Cells["setor_codigo"].Value);
            if (codorig.ToString() == codgrid.ToString())
            {
                achou = 1;
                break;
            }
            else
            {
                achou = 0;
               // break;
            }
        }

        if (achou == 0)
        {
            //inclui o setor nao encointrado no grid
            MessageBox.Show(linha["Codigo"].ToString());

        }
    }
}
    
21.11.2017 / 15:48