日期:2014-05-17  浏览次数:20556 次

如何用遍历的方法计算gridview每行的总计
我想要的效果是

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

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


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

------解决方案--------------------
C# code
        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;//最好转化后相加
            }
        }

------解决方案--------------------
参考
HTML code
<%@ 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