I need to fill out a DataGridView
that already contains a list of all the items. This based on movement records of materials contained in a DataSet
. The DataGridView
already has all items, for example: A, B, C, D, etc. And the program will check the current balance of each item based on movements of a stock contained in the DataSet
.
I thought to do a foreach inside the other. The DataGridView
out line by line to find the name of the item and the other internal foreach makes the search in dataSet where the movement of a stock are recorded.
I made it this way, but it is filling only the first line of the Balance
column.
void FillDataGrid()
{
var result = 0;
string strRowMaterial = string.Empty;
Conection ca = new Conection();
string sql = "";
sql += " Select ";
sql += " idRegMovement, CatMovement, NameMovement, codMaterial, AmountMovemented ";
sql += " From RegMovement ";
sql += " ORDER BY idRegMovement ";
ca.Connect();
OleDbDataAdapter da = new OleDbDataAdapter(sql, ca.cx);
DataSet ds = new DataSet();
da.Fill(ds, "RegMovements");
if (ds.Tables["RegMovements"].Rows.Count == 0)
{
ca.Disconnect();
}
else
{
foreach (DataGridViewRow dr in datagridview.Rows)
{
strRowMaterial = Convert.ToString(dr.Cells["codMaterial"].Value);
foreach (DataRow row in ds.Tables["RegMovements"].Rows)
{
if (string.Compare(row["codMaterial"].ToString(), strRowMaterial, StringComparison.InvariantCultureIgnoreCase) == 0)
{
var Amount = Convert.ToInt32(row["AmountMovemented"]);
if (string.Compare(row["CatMovement"].ToString(), "Input", StringComparison.InvariantCultureIgnoreCase) == 0)
result += Amount;
else
result -= Amount;
}
else
{
result = 0;
}
}
datagridview.CurrentRow.Cells["Balance"].Value = result;
}
}
ca.Disconnect();
}
Regards!
When adding the value you use only 1 row CurrentRow
datagridview.CurrentRow.Cells["Balance"].Value = result;
if you foreach through the:
foreach (DataGridViewRow dr in datagridview.Rows)
you should also use the variable:
dr.Cells["Balance"].Value = result;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.