Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Tuesday, 14 April 2015

Introduction: 

In this Article I will explain how to show or display sum of columns total in gridview footer using asp.net in c#, vb.net.

Implementation:  Let's create a demo website to demonstrate the concept.
Create a database “Test” and then create one table i.e. “EmDetails”
Column Name
Data Type
Allow Nulls
Empid
Int(IDENTITY=TRUE)
Yes
Name
varchar(50)
Yes
Salary
int
Yes
Source Code: Sample Application
Design Page: design your page as given below

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Show total in Gridview Footer</title>
</head>
<body>
<form id="form1" runat="server">
<fieldset style="width:300px">
    <legend><strong>Show total in Gridview Footer</strong></legend>
<div class="GridviewDiv">
<asp:GridView runat="server" ID="gvDetails" ShowFooter="True" AutoGenerateColumns="false" Width="350px" >
  <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
  <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="EmpId" HeaderText="Employee Id" />
<asp:BoundField DataField="Name" HeaderText="Employee Name" />
<asp:BoundField DataField="Salary" HeaderText="Salary" />
</Columns>
</asp:GridView>
</div></fieldset>
</form>
</body>
</html>

ASP.NET Code behind File (c#):
Write following code in code behind file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.WebControls;

public partial class paging : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridview();
        }
    }
    protected void BindGridview()
    {
        DataTable ds = new DataTable();
        using (SqlConnection con = new SqlConnection("Data Source=localhost;Integrated Security=true;Initial Catalog=Test"))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from EmDetails", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            con.Close();
            gvDetails.DataSource = ds;
            gvDetails.DataBind();
            gvDetails.FooterRow.Cells[1].Text = "Total Amount";
            gvDetails.FooterRow.Cells[2].Text = ds.Compute("Sum(Salary)", "").ToString();
        }
    } 
}

ASP.NET Code behind File (VB.NET):
Write following code in code behind file:

Imports System.Data.SqlClient
Imports System.Data
Imports System.Web.UI.WebControls

Partial Public Class paging
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Not IsPostBack Then
            BindGridview()
        End If
    End Sub
    Protected Sub BindGridview()
        Dim ds As DataTable = New DataTable ()
        Imports (SqlConnection con = New SqlConnection("Data Source=localhostIntegrated Security=TrueInitial Catalog=Test"))
        {
        con.Open()
        Dim cmd As SqlCommand = New SqlCommand("select * from Emp_Personal", con)
        Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
        da.Fill(ds)
        con.Close()
        gvDetails.DataSource = ds
        gvDetails.DataBind()
gvDetails.FooterRow.Cells(1).Text = "Total Amount"
gvDetails.FooterRow.Cells(2).Text = dt.Compute("Sum(price)", "").ToString()

        }
    End Sub

End Class
Categories: , ,

0 comments:

Post a Comment