How to browse a Dataset based on the data contained in a Datagridview?

Asked

Viewed 1,369 times

1

I need to fill out a datagridview which already contains the list of all items. This is based on the records of material movements in a DataSet. The datagridview already has all items, for example: A, B, C, D., etc. And the program will consult the current balance of item by item based on the movements in DataSet.

I thought I’d make a foreach inside the other. A precorre o datagridview, line by line to fetch the item name and the other foreach internal search in the dataSet where the movements are registered.

I did it this way, but it’s just filling the first row of the Balance column.

void VarreDataGrid()
    {
        var resultado = 0;
        string strRowMaterial = string.Empty;

        Conexao ca = new Conexao();
        string sql = "";
        sql += " Select ";
        sql += " idRegMovimentacao, CatMovimentacao, NomeMovimentacao, codMaterial, QuantidadeMovimentada ";
        sql += " From RegistrosMovimentacao ";
        sql += " ORDER BY idRegMovimentacao ";

        ca.Conectar();
        OleDbDataAdapter da = new OleDbDataAdapter(sql, ca.cx);
        DataSet ds = new DataSet();
        da.Fill(ds, "RegistrosMovimentacoes");
        if (ds.Tables["RegistrosMovimentacoes"].Rows.Count == 0)
        {
            ca.Desconectar();
        }
        else
        {

            foreach (DataGridViewRow dr in dgvListagem.Rows)
            {
                strRowMaterial = Convert.ToString(dr.Cells["codMaterial"].Value);

                foreach (DataRow row in ds.Tables["RegistrosMovimentacoes"].Rows)
                {
                    if (string.Compare(row["codMaterial"].ToString(), strRowMaterial, StringComparison.InvariantCultureIgnoreCase) == 0)
                    {
                        var quantidade = Convert.ToInt32(row["QuantidadeMovimentada"]);

                        if (string.Compare(row["CatMovimentacao"].ToString(), "Entrada", StringComparison.InvariantCultureIgnoreCase) == 0)
                            resultado += quantidade;

                        else
                            resultado -= quantidade; 
                    }
                    else
                    {
                        resultado = 0;
                    }                        

                }
                dgvListagem.CurrentRow.Cells["Saldo"].Value = resultado;
            }
        }

        ca.Desconectar();

    }

1 answer

0


The mistake was in:

dgvListage.CurrentRow.Cells["Balance"]. Value = result;

It worked as follows:

    void VarreDataGrid()
    {
        var resultado = 0;
        string strRowMaterial = string.Empty;

        Conexao ca = new Conexao();
        string sql = "";
        sql += " Select ";
        sql += " idRegMovimentacao, CatMovimentacao, NomeMovimentacao, codMaterial, QuantidadeMovimentada ";
        sql += " From RegistrosMovimentacao ";
        sql += " ORDER BY idRegMovimentacao ";

        ca.Conectar();
        OleDbDataAdapter da = new OleDbDataAdapter(sql, ca.cx);
        DataSet ds = new DataSet();
        da.Fill(ds, "RegistrosMovimentacoes");
        if (ds.Tables["RegistrosMovimentacoes"].Rows.Count == 0)
        {
            ca.Desconectar();
        }
        else
        {

            foreach (DataGridViewRow dr in dgvListagem.Rows)
            {
                strRowMaterial = Convert.ToString(dr.Cells["codMaterial"].Value);

                foreach (DataRow row in ds.Tables["RegistrosMovimentacoes"].Rows)
                {
                    if (string.Compare(row["codMaterial"].ToString(), strRowMaterial, StringComparison.InvariantCultureIgnoreCase) == 0)
                    {
                        var quantidade = Convert.ToInt32(row["QuantidadeMovimentada"]);

                        if (string.Compare(row["CatMovimentacao"].ToString(), "Entrada", StringComparison.InvariantCultureIgnoreCase) == 0)
                            resultado += quantidade;

                        else
                            resultado -= quantidade; 
                    }
                    else
                    {
                        resultado = 0;
                    }                        

                }

            }
                dr.Cells["Saldo"].Value = resultado;
        }

        ca.Desconectar();

    }

Abs!

Browser other questions tagged

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