在GridView的页脚中显示总计,并在最后一列中添加列的总和(行虎钳)

在我的Asp.net应用程序中,我有一个GridView ,我自己使用后面的代码生成column[6]的数据。

通过查看下面的代码,我的gridview有一个footer 。 如果我使用页脚,问题是列[6]的文本将不会出现。 如果我删除了footertext代码,那么我的列[6]的文本就出现了。 问题是什么? 两个代码都不能使用togather? 我已经设置了ShowFooter =“True”

 protected void Page_Load(object sender, EventArgs e) { for (int i = 0; i < (this.GridView1.Rows.Count); i++) { this.GridView1.Rows[i].Cells[6].Text = "testing"; //GridView1.Columns[1].FooterText ="footer 1"; } } 

的.aspx

                     

示例代码 :以编程方式设置页脚文本

  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Footer) { Label lbl = (Label)e.Row.FindControl("lblTotal"); lbl.Text = grdTotal.ToString("c"); } } 

更新的代码

  decimal sumFooterValue = 0; protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string sponsorBonus = ((Label)e.Row.FindControl("Label2")).Text; string pairingBonus = ((Label)e.Row.FindControl("Label3")).Text; string staticBonus = ((Label)e.Row.FindControl("Label4")).Text; string leftBonus = ((Label)e.Row.FindControl("Label5")).Text; string rightBonus = ((Label)e.Row.FindControl("Label6")).Text; decimal totalvalue = Convert.ToDecimal(sponsorBonus) + Convert.ToDecimal(pairingBonus) + Convert.ToDecimal(staticBonus) + Convert.ToDecimal(leftBonus) + Convert.ToDecimal(rightBonus); e.Row.Cells[6].Text = totalvalue.ToString(); sumFooterValue += totalvalue } if (e.Row.RowType == DataControlRowType.Footer) { Label lbl = (Label)e.Row.FindControl("lblTotal"); lbl.Text = sumFooterValue.ToString(); } } 

在.aspx页面

                                                                         

我的博客 – Asp.net Gridview文章

这可以通过LINQ with grouping实现,这里是指向实际网格视图的数据源的项列表。 示例伪代码可以帮助编码实际。

 var tabelDetails =(from li in dc.My_table join m in dc.Table_One on li.ID equals m.ID join c in dc.Table_two on li.OtherID equals c.ID where //Condition group new { m, li, c } by new { m.ID, m.Name } into g select new { g.Key.ID, Name = g.Key.FullName, sponsorBonus= g.Where(s => scName == "sponsorBonus").Count(), pairingBonus = g.Where(s => scName == "pairingBonus").Count(), staticBonus = g.Where(s => scName == "staticBonus").Count(), leftBonus = g.Where(s => scName == "leftBonus").Count(), rightBonus = g.Where(s => scName == "rightBonus").Count(), Total = g.Count() //Row wise Total }).OrderBy(t => t.Name).ToList(); tabelDetails.Insert(tabelDetails.Count(), new //This data will be the last row of the grid { Name = "Total", //Column wise total sponsorBonus = tabelDetails.Sum(s => s.sponsorBonus), pairingBonus = tabelDetails.Sum(s => s.pairingBonus), staticBonus = tabelDetails.Sum(s => s.staticBonus), leftBonus = tabelDetails.Sum(s => s.leftBonus), rightBonus = tabelDetails.Sum(s => s.rightBonus ), Total = tabelDetails.Sum(s => s.Total) });