Step 1:- Javascript code
<script type="text/javascript">
function Calculate()
{
var GridView1
= document.getElementById("GridView1");
var sum
= 0;
for (var i
= 0; i < GridView1.rows.length; i++) {
// i =
0 is header row of griedview
if (i
!= 0) {
sum
+= parseInt(GridView1.rows[i].cells[4].innerText);
}
}
alert("Sum
of " + sum);
}
</script>
Step 2:- Server side code
var sum
= 0;
foreach (GridViewRow row in GridView1.Rows)
{
sum
+= Convert.ToInt32(row.Cells[4].Text);
}
Step 3:- Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Demo.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function Calculate()
{
var GridView1
= document.getElementById("GridView1");
var sum
= 0;
for (var i
= 0; i < GridView1.rows.length; i++) {
// i =
0 is header row of griedview
if (i
!= 0) {
sum
+= parseInt(GridView1.rows[i].cells[4].innerText);
}
}
alert("Sum
of " + sum);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnCalculate" runat="server" Text="Calculate" OnClick="btnCalculate_Click" />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ClientIDMode="Static">
<Columns>
<asp:BoundField DataField="Column1" HeaderText="Column1" />
<asp:BoundField DataField="Column2" HeaderText="Column2" />
<asp:BoundField DataField="Column3" HeaderText="Column3" />
<asp:BoundField DataField="Column4" HeaderText="Column4" />
<asp:BoundField DataField="Column5" HeaderText="Column5" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Step 4:- Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Demo
{
public partial class Default:
System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
btnCalculate.Attributes.Add("onclick", "return
Calculate();");
GridView1.DataSource
= GenerateData();
GridView1.DataBind();
}
}
private DataTable GenerateData()
{
DataTable dt
= new DataTable();
DataRow dr
= null;
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dt.Columns.Add(new DataColumn("Column4", typeof(string)));
dt.Columns.Add(new DataColumn("Column5", typeof(string)));
int totalRow
= 25;
for (int i
= 0; i < totalRow; i++)
{
dr
= dt.NewRow();
dr["Column1"] = i
+ 1;
dr["Column2"] = "Row" +
i;
dr["Column3"] = "Row" +
i;
dr["Column4"] = DateTime.Now.ToString("dd/MM/yyyy
hh:mm:ss:ms tt");
dr["Column5"] =
5000 / (i + 2);
dt.Rows.Add(dr);
}
return dt;
}
protected void btnCalculate_Click(object sender, EventArgs e)
{
var sum
= 0;
foreach (GridViewRow row in GridView1.Rows)
{
sum
+= Convert.ToInt32(row.Cells[4].Text);
}
DisplayMessage("Sum
of " + sum);
}
protected void DisplayMessage(string strMsg)
{
strMsg
= strMsg.Replace("\r\n", "\\n");
strMsg
= strMsg.Replace("\n", "\\n");
strMsg
= strMsg.Replace("'", "");
string scriptString
= @"<script
language=""JavaScript"">";
scriptString
+= "alert('" + strMsg + "');";
scriptString
+= "</script>";
ClientScript.RegisterStartupScript(this.GetType(), "Startup",
scriptString);
}
}
}
No comments:
Post a Comment