Example:
GridView:
<asp:GridView ID="GridDados" AutoGenerateColumns="False" runat="server" ItemType="WebAppDiegoWebForms.Fulano" OnRowDataBound="GridDados_RowDataBound" ShowFooter="True">
<Columns>
<asp:BoundField DataField="Codigo" HeaderText="Código" DataFormatString="{0:0000}"/>
<asp:BoundField DataField="Nome" HeaderText="Nome" />
<asp:BoundField DataField="Data" DataFormatString="{0:d}" HeaderText="Data" />
<asp:BoundField DataField="Quantidade" HeaderText="Quantidade" />
<asp:BoundField DataField="Valor" DataFormatString="{0:N2}" HeaderText="Valor" />
<asp:BoundField DataField="ValorParcial" DataFormatString="{0:N2}" HeaderText="Valor Parcial" />
</Columns>
</asp:GridView>
In your GridDados you have the columns Amount , Amount and Amount and no Footer
will be summing all ValueValue values.
How to:
Class Template:
public class Fulano
{
public int Codigo { get; set; }
public string Nome { get; set; }
public DateTime Data { get; set; }
public Decimal Valor { get; set; }
public int Quantidade { get; set; }
}
Code of this WebForm
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var valores = (new Fulano[]
{
new Fulano { Codigo = 1, Nome = "Fulano 1", Data = DateTime.Now, Valor = 100M, Quantidade = 2},
new Fulano { Codigo = 2, Nome = "Fulano 2", Data = DateTime.Now, Valor = 200M, Quantidade = 1},
new Fulano { Codigo = 3, Nome = "Fulano 3", Data = DateTime.Now, Valor = 100M, Quantidade = 1},
new Fulano { Codigo = 4, Nome = "Fulano 4", Data = DateTime.Now, Valor = 150M, Quantidade = 1},
new Fulano { Codigo = 5, Nome = "Fulano 5", Data = DateTime.Now, Valor = 300M, Quantidade = 1}
})
.Select(x => new
{
x.Codigo,
x.Nome,
x.Data,
x.Valor,
x.Quantidade,
ValorParcial = (x.Valor * x.Quantidade)
})
.ToArray();
GridDados.DataSource = valores;
GridDados.DataBind();
}
}
private decimal total = 0;
private decimal valor = 0;
protected void GridDados_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
valor = 0;
if (decimal.TryParse(e.Row.Cells[5].Text, out valor)){
total += valor;
}
} else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[4].Text = "Total:";
e.Row.Cells[5].Text = total.ToString("N2");
total = 0;
}
}
The load in this example is manual, but you can use it your way. The only requirement is that before sending to% GridData I do a GridView
multiplication and send the data to all formatted grid.
No ValorParcial = (x.Valor * x.Quantidade)
I do the proper sum operations and in the end I show in the GridView Footer. Note: Variables of type GridDados_RowDataBound
total and value are placed outside the decimal
/ p>
Result: