Monday 6 January 2014

Show Column total in Gridview footer

Aspx Page:
------------
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="GridView1" AutoGenerateColumns="false" ShowFooter="true"
            runat="server" AllowPaging="True">
            <Columns>
                <asp:TemplateField HeaderText="ProductID">
                    <ItemTemplate>
                        <asp:Label ID="lblProductID" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ProductName">
                    <ItemTemplate>
                        <asp:Label ID="lblProductName" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:Label ID="lblTotal" Font-Bold="true" runat="server" Text="Total"></asp:Label>
                    </FooterTemplate>
                </asp:TemplateField>
                 <asp:TemplateField HeaderText="UnitPrice">
                    <ItemTemplate>
                        <asp:Label ID="lblUnitPrice" runat="server" Text='<%# Eval("UnitPrice") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:Label ID="lblTotalUnitPrice" Font-Bold="true" runat="server"></asp:Label>
                    </FooterTemplate>
                </asp:TemplateField>
                 <asp:TemplateField HeaderText="UnitsInStock">
                    <ItemTemplate>
                        <asp:Label ID="lblUnitsInStock" runat="server" Text='<%# Eval("UnitsInStock") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:Label ID="lblTotalUnitsInStock" Font-Bold="true" runat="server"></asp:Label>
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
   
    </div>
    </form>
</body>
</html>


.VB Page
--------
Imports System.Data
Imports System.Data.SqlClient

Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        GridView1.DataSource = ProductsData()
        GridView1.DataBind()
    End Sub

    Public Function ProductsData() As DataSet
        Dim text As String = "select   ProductID, ProductName, UnitPrice, UnitsInStock  from ProductMaster"

        Dim connString As String = "Data Source=YourServer;Initial Catalog=your database;User ID=***;Password=***;trusted_Connection=false; Connect timeout=0;"
        Dim conn As New SqlConnection(connString)
        Dim cmd As New SqlCommand(text, conn)
        conn.Open()
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet()
        da.Fill(ds)
        conn.Close()
        Return ds
    End Function

    Private totalUnitPrice As Decimal = 0
    Private totalUnitInstock As Integer = 0

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            totalUnitPrice += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "UnitPrice"))
            totalUnitInstock += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "UnitsInStock"))
        End If
        If e.Row.RowType = DataControlRowType.Footer Then
            Dim lblGTUnitPrice As Label = DirectCast(e.Row.FindControl("lblTotalUnitPrice"), Label)
            Dim lblGTUnitInStock As Label = DirectCast(e.Row.FindControl("lblTotalUnitsInStock"), Label)
            lblGTUnitPrice.Text = totalUnitPrice.ToString()
            lblGTUnitInStock.Text = totalUnitInstock.ToString()
        End If
    End Sub

    Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs) Handles GridView1.PageIndexChanging
        GridView1.PageIndex = e.NewPageIndex
        GridView1.DataSource = ProductsData()
        GridView1.DataBind()
    End Sub
End Class