是否可以添加两个数据行并将结果输入c#中相同数据表的第三个数据行? 此外,是否可以将两个数据行绑定到结果行,以便在任何值更改时,它会反映在结果行中?
提前致谢
答案 0 :(得分:0)
使用DataTable编辑的响应,我还没有测试过这段代码。
public static class DataTableHandler
{
private const string COL_PRICE = "PriceColumn";
private const string COL_QUANTITY = "QuantityColumn";
public static DataTable AddTotalRow(DataTable dataTable)
{
int totalQuantity = 0;
decimal totalPrice = 0.0;
CalculateTotals(dataTable, out totalQuantity, out totalPrice);
DataRow row = dataTable.AddRow();
row.Cells[COL_QUANTITY].Value = totalQuantity;
row.Cells[COL_PRICE].Value = totalPrice;
dataTable.Rows.Add(row);
return dataTable;
}
private static void CalculateTotals(DataTable dataTable, out int totalQuantity, out decimal totalPrice)
{
totalPrice = 0.0;
totalQuantity = 0;
foreach (DataRow r in dataTable.Rows)
{
totalPrice += (decimal)r.Cells[COL_PRICE].Value;
totalQuantity += (int)r.Cells[COL_QUANTITY].Value;
}
}
}