<< Click to Display Table of Contents >> GridView - Footer - somatório |
![]() ![]() ![]() |
Tela
aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="teste2.aspx.cs" Inherits="Visual.teste2" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="/css/bootstrap.min.css" rel="stylesheet" media="screen" />
</head>
<body>
<form id="form1" runat="server">
<div style="width: 60%; padding: 50px;">
<asp:GridView ID="GridView1" ShowFooter="true" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" CssClass="table table-striped table-bordered table-condensed">
<Columns>
<asp:BoundField DataField="Nome" HeaderText="Nome" />
<asp:BoundField DataField="Unit" DataFormatString="{0:0.00}" HeaderText="Valor" ItemStyle-HorizontalAlign="Right" />
<asp:TemplateField HeaderText="Qtd" ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:Label ID="lbQtd" runat="server" Text='<%#Eval("Qtd") %>' />
<!-- ACIMA MODO NÃO RECOMENDADO, ABAIXO MODO MAIS CORRETO (MAS SÓ FUNCIONA COM DATATABLE -->
<asp:Label ID="Label1" runat="server" Text='<%#((DataRowView)Container.DataItem)["Qtd"]%>' /> -->
</ItemTemplate>
<FooterTemplate>
<strong>
<asp:Label ID="lbQtd" runat="server" Text="Total" />
</strong>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total" ItemStyle-HorizontalAlign="Right">
<FooterStyle HorizontalAlign="Right" />
<ItemTemplate>
<asp:Label ID="lbTotal" runat="server" Text="0.00" />
</ItemTemplate>
<FooterTemplate>
<strong>
<asp:Label ID="lbTotal" runat="server" Text="0.00" />
</strong>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
cs
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
namespace Visual
{
public class Dado
{
public string Nome { get; set; }
public decimal Unit { get; set; }
public int Qtd { get; set; }
public decimal Total
{
get { return Unit * Qtd; }
}
public static List<Dado> Get()
{
List<Dado> lista = new List<Dado>();
lista.Add(new Dado() { Nome = "Junior", Unit = (decimal)10.02, Qtd = 1 });
lista.Add(new Dado() { Nome = "Flavio", Unit = (decimal)98.10, Qtd = 3 });
lista.Add(new Dado() { Nome = "Mario", Unit = (decimal)43.45, Qtd = 4 });
lista.Add(new Dado() { Nome = "Luidi", Unit = (decimal)99.25, Qtd = 5 });
lista.Add(new Dado() { Nome = "Peach", Unit = (decimal)76.44, Qtd = 6 });
lista.Add(new Dado() { Nome = "Toad", Unit = (decimal)98.30, Qtd = 7 });
lista.Add(new Dado() { Nome = "Rosalina", Unit = (decimal)25.12, Qtd = 8 });
lista.Add(new Dado() { Nome = "Inara", Unit = (decimal)78.99, Qtd = 9 });
return lista;
}
}
public partial class teste2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = Dado.Get();
GridView1.DataBind();
}
}
decimal total = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[1].Attributes.Add("style", "text-align: right");
e.Row.Cells[2].Attributes.Add("style", "text-align: right");
e.Row.Cells[3].Attributes.Add("style", "text-align: right");
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbTotal = (Label)e.Row.FindControl("lbTotal");
Dado d = (Dado)e.Row.DataItem;
// DataRowView drv = (DataRowView)e.Row.DataItem;
// decimal valor = Convert.ToDecimal(drv["Unit"].ToString()) * Convert.ToInt32(drv["Qtd"].ToString());
decimal valor = d.Unit * d.Qtd;
lbTotal.Text = valor.ToString("0.00");
total += valor;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lbTotal = (Label)e.Row.FindControl("lbTotal");
lbTotal.Text = total.ToString("#,##0.00");
e.Row.Cells[2].Attributes.Add("style", "background-color: LightYellow");
e.Row.Cells[3].Attributes.Add("style", "background-color: LightYellow");
}
}
}
}