compare datatable with gridview

Asked

Viewed 90 times

0

my question is this: i have a gridview that I populate it with a data table of a table called "Discountdoclient", but I need a button that when it is clicked it goes in another table called "sectors" and go through each row of the table sectors and then check if the sector found in the table "sectors" is already in datagrid.

I tried it like this, but I’m not having any 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
        }
    }
}
  • In this code you happen some error or simply it does not return anything? And just a doubt, because you are converting to int and then to string?

  • 1

    it does not give error, but tb does not work... this grid is filled with the sectors of the store in which the customer has discount, but sometimes some sector is erased because he has no more right to discount in that sector. So far so good, but from time to time, we have to put the sectors back to him, but only those that he still does not have. then this code would be for this: check if the grid has all the available sectors and if not, include the missing...

  • the idea was to go through the sectors table, regsitro by record... and when I took the sector code, check if this code is already in the grid view and if it is not there I would include only it, the missing.

1 answer

0

Solved!!!!

It was an over-tempered BREACK...

I was just saying that it worked...

As I myself have resolved, what is the procedure for closing this topic?

Thank you 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());

        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.