如何用遍历的方法计算gridview每行的总计

abz877 2012-05-02 02:23:52
我想要的效果是

列1 列2 列3 总计
行1
行2
行3

foreach (GridViewRow gvr in GridView1.Rows)
{
foreach (TableCell gvCell in gvr.Cells)
{
}
}


如何用遍历的方法计算gridview每行的总计?
...全文
156 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2012-05-02
  • 打赏
  • 举报
回复
参考
<%@ Page Language="C#" AutoEventWireup="true" %>

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

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{

System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("ProductCode", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("ProductName", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("MallPrice", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("Num", typeof(System.Int32)));
dt.Columns.Add(new System.Data.DataColumn("Intergral", typeof(System.Int32)));
dt.Columns.Add(new System.Data.DataColumn("ProImage", typeof(System.String)));

System.Random rd = new System.Random(Environment.TickCount); ;

for (int i = 0; i < 8; i++)
{
dr = dt.NewRow();
dr[0] = "孟" + i.ToString();
dr[1] = "孟孟" + i.ToString();
dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[3] = rd.Next(9999);
dr[4] = i;
dr[5] = "http://dotnet.aspx.cc/Images/meng.gif";
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
TotalPrice.Text = totalCount.ToString();
}
}


decimal totalCount = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
String n = DataBinder.Eval(e.Row.DataItem, "Num").ToString();
String price = DataBinder.Eval(e.Row.DataItem, "MallPrice").ToString();
int num = 0;
Int32.TryParse(n, out num);
decimal MallPrice = Convert.ToDecimal(price);
totalCount += num * MallPrice;
e.Row.Cells[6].Text = (num * MallPrice).ToString();
}
}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Response.Write("执行SQL DELETE ProductCode=" + GridView1.DataKeys[e.RowIndex].Value.ToString());
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript">
function jia(ele) {
tr = ele.parentNode;
while (tr.nodeType != 1 || tr.tagName != "TR") tr = tr.parentNode;
num = tr.cells[5].getElementsByTagName("input")[0];
var t = parseInt(num.value, 10);
if (isNaN(t)) num.value = 0;
else num.value = t + 1;
countRow(tr)
}
function jian(ele) {
tr = ele.parentNode;
while (tr.nodeType != 1 || tr.tagName != "TR") tr = tr.parentNode;
num = tr.cells[5].getElementsByTagName("input")[0];
var t = parseInt(num.value, 10);
if (isNaN(t)) num.value = 0;
else {
if (t < 1) return;
num.value = t - 1;
}
countRow(tr)
}
function bian(ele) {
tr = ele.parentNode;
while (tr.nodeType != 1 || tr.tagName != "TR") tr = tr.parentNode;
countRow(tr)
}

function countRow(row) {
price = parseFloat(row.cells[3].innerHTML);
if (isNaN(price)) {
row.cells[6].innerHTML = "0"
return;
}
num = row.cells[5].getElementsByTagName("input")[0];
t = parseInt(num.value, 10);
if (isNaN(t)) t = 0;
row.cells[6].innerHTML = roundPrice(price * t);
CountAll();
}
function CountAll() {
var total = 0;
table = document.getElementById('<%=GridView1.ClientID %>');
if (table.rows.length < 3) return;
for (i = 1; i < table.rows.length; i++) {
p = parseFloat(table.rows[i].cells[6].innerHTML);
if (isNaN(p)) p = 0;
total += p;
}
document.getElementById('<%=TotalPrice.ClientID %>').innerHTML = roundPrice(total);
}

function roundPrice(x) {
return Math.round(x * 100) / 100;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<table>
<tbody>
<tr class="biao"><td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductCode"
Width="1052px" CssClass="No" OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:BoundField DataField="ProductCode" HeaderText="商品编号"></asp:BoundField>
<asp:ImageField DataAlternateTextField="ProImage" DataImageUrlField="ProImage" HeaderText="商品图片">
</asp:ImageField>
<asp:BoundField DataField="ProductName" HeaderText="商品名称" />
<asp:TemplateField HeaderText="价格">
<ItemTemplate>
<%#Eval("MallPrice") %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Intergral" HeaderText="赠送积分" />
<asp:TemplateField HeaderText="商品数量">
<ItemTemplate>
<a href="#" onclick="jian(this);return false;">-</a>
<input type="text" id="num" value='<%#Eval("Num")%>' onchange="bian(this)" />
<a href="#" onclick="jia(this);return false;">+</a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="小计">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="删除次商品" ShowDeleteButton="True">
<ControlStyle BorderWidth="0px" Width="40px" />
</asp:CommandField>
</Columns>
</asp:GridView>
</td></tr>
</tbody>
<tfoot>
<tr><td colspan="6">
<asp:Label ID="Label1" runat="server"></asp:Label>
商品总金额: <span>¥<asp:Label ID="TotalPrice" runat="server"></asp:Label></span>元</td>
</tr>
</table>
</form>
</body>
</html>
cupsBing 2012-05-02
  • 打赏
  • 举报
回复
$("#gridview1 td #lump").each(function(){
//如果总计ID为lump,JS遍历不知道可以吗
});

for(int i=0;i<gridview1.rows.count;i++)
{
//如果总计的标签是lbl1
Label lbl = gridview.findcontrol["lbl1"] as Label;
//接下来
}
  • 打赏
  • 举报
回复
        for (int i = 0; i < GridView1.Rows.Count; i++)
{
for (int j = 0; j < GridView1.Columns.Count; j++)
{
GridView1.Rows[i].Cells[3].Text = 列1+列2+列3;//最好转化后相加
}
}

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧