I need to fill in a datagridview
that already contains the relation of all the items. This is based on material movement records that are in DataSet
. datagridview
already has all the items, like for example: A, B, C, D., etc. And the program will check the current balance of item by item based on the movements that are in DataSet
.
I thought about doing a foreach
within the other. One prepends the datagridview
, line by line to search the name of the item and the other foreach
internal searches the dataSet
where the transactions are registered.
I did it this way, but it's only 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();
}