<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"

CellPadding="3" DataKeyNames="ProductID" DataSourceID="SqlDataSource1"

GridLines="Vertical" ondatabound="GridView1_DataBound" ShowFooter="True">

<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />

<RowStyle BackColor="#EEEEEE" ForeColor="Black" />

<Columns>

<asp:BoundField DataField="ProductID" HeaderText="ProductID"

InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />

<asp:BoundField DataField="ProductName" HeaderText="ProductName"

SortExpression="ProductName" />

<asp:BoundField DataField="UnitPrice"

HeaderText="UnitPrice" SortExpression="UnitPrice" DataFormatString="{0:c}"

HtmlEncode="False" />

<asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock"

SortExpression="UnitsInStock" />

</Columns>

<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />

<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />

<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />

<AlternatingRowStyle BackColor="#DCDCDC" />

</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"

ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"

SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice], [UnitsInStock] FROM [Products] WHERE ([CategoryID] = @CategoryID)">

<SelectParameters>

<asp:Parameter DefaultValue="2" Name="CategoryID" Type="Int32" />

</SelectParameters>

</asp:SqlDataSource>

 

 

protected void GridView1_DataBound(object sender, EventArgs e)

{

decimal valorEstoque = 0;

foreach (GridViewRow row in GridView1.Rows)

{

decimal preco = Decimal.Parse(row.Cells[2].Text.Replace("R$",String.Empty));

int estoque = Int32.Parse(row.Cells[3].Text);

valorEstoque += preco * estoque;

}

GridViewRow footer = GridView1.FooterRow;

footer.Cells[0].ColumnSpan = 3;

footer.Cells[0].HorizontalAlign = HorizontalAlign.Center;

//Remove as c?lulas não utilizadas

footer.Cells.RemoveAt(2);

footer.Cells.RemoveAt(1);

//Adiciona um texto

footer.Cells[0].Text = "Valor total: " + valorEstoque.ToString("C");

}

 

Fabio Galante Mans
www.netitc.com.br - Hospedagem para Desenvolvedores
www.mans.com.br