Код: Выделить всё
< /code>
Вот соответствующий код C#: < /p>
SqlCommand cmd = new SqlCommand(sql, conn);
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
conn.Open();
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
if (budgetType == "Department")
{
DeptBudgetView.DataSource = dt;
DeptBudgetView.DataBind();
// added on 7/30/25 to calculate totals for the footer row
// getting an InvalidCastException here
decimal totalAllocated = dt.AsEnumerable().Sum(row => row.Field("amtAllocated") ?? 0);
decimal totalSpent = dt.AsEnumerable().Sum(row => row.Field("amtSpent") ?? 0);
decimal totalRemaining = dt.AsEnumerable().Sum(row => row.Field("amtRemaining") ?? 0);
DeptBudgetView.FooterRow.Cells[0].Text = "Totals:";
DeptBudgetView.FooterRow.Cells[1].Text = totalAllocated.ToString("C");
DeptBudgetView.FooterRow.Cells[2].Text = totalSpent.ToString("C");
DeptBudgetView.FooterRow.Cells[3].Text = totalRemaining.ToString("C");
DeptBudgetView.FooterRow.Cells[0].HorizontalAlign = HorizontalAlign.Right;
}
else // Vendor Budget
{
VendorBudgetView.DataSource = dt;
VendorBudgetView.DataBind();
// added on 7/30/25 to calculate totals for the footer row
decimal totalAllocated = dt.AsEnumerable().Sum(row => row.Field("amtAllocated") ?? 0);
decimal totalSpent = dt.AsEnumerable().Sum(row => row.Field("amtSpent") ?? 0);
decimal totalRemaining = dt.AsEnumerable().Sum(row => row.Field("amtRemaining") ?? 0);
VendorBudgetView.FooterRow.Cells[0].Text = "Totals:";
VendorBudgetView.FooterRow.Cells[1].Text = totalAllocated.ToString("C");
VendorBudgetView.FooterRow.Cells[2].Text = totalRemaining.ToString("C");
VendorBudgetView.FooterRow.Cells[0].HorizontalAlign = HorizontalAlign.Right;
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... a-gridview