Hi Sriram,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="True">
<Columns>
<asp:TemplateField ControlStyle-Width="20px">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="True" OnCheckedChanged="chkSelect_CheckedChanged">
</asp:CheckBox>
</ItemTemplate>
<ItemStyle Width="20px" />
<ControlStyle Width="20px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="OrderID">
<ItemTemplate>
<asp:Label ID="lblOrderID" runat="server" Text="<%#Bind('OrderID') %>"></asp:Label>
</ItemTemplate>
<ItemStyle VerticalAlign="Bottom" />
</asp:TemplateField>
<asp:TemplateField HeaderText="CustomerID">
<ItemTemplate>
<asp:Label ID="lblCustomerID" runat="server" Text="<%#Bind('CustomerID') %>"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="Label1" runat="server" Text="Total" Font-Bold="true" />
</FooterTemplate>
<ItemStyle VerticalAlign="Bottom" />
<FooterStyle HorizontalAlign="Right" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Freight">
<ItemTemplate>
<asp:TextBox ID="txtFreight" runat="server" Text="<%#Bind('Freight','{0:N2}') %>"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server" Text="0" Font-Bold="true" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#333333" />
</asp:GridView>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT TOP 10 OrderID,CustomerID,Freight FROM Orders"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
}
protected void chkSelect_CheckedChanged(object sender, EventArgs e)
{
decimal total = 0;
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chk = row.FindControl("chkSelect") as CheckBox;
TextBox freight = row.FindControl("txtFreight") as TextBox;
if (chk.Checked)
{
total += Convert.ToDecimal(freight.Text);
}
}
(GridView1.FooterRow.FindControl("lblTotal") as Label).Text = total.ToString("N2");
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT TOP 10 OrderID,CustomerID,Freight FROM Orders")
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Using
End Using
End Using
End If
End Sub
Protected Sub chkSelect_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim total As Decimal = 0
For Each row As GridViewRow In GridView1.Rows
Dim chk As CheckBox = TryCast(row.FindControl("chkSelect"), CheckBox)
Dim freight As TextBox = TryCast(row.FindControl("txtFreight"), TextBox)
If chk.Checked Then
total += Convert.ToDecimal(freight.Text)
End If
Next
TryCast(GridView1.FooterRow.FindControl("lblTotal"), Label).Text = total.ToString("N2")
End Sub
Screenshot