我使用以下语法来操作gridview的RowDataBound
和RowCreated
方法,以便每次更改employeeid以添加总行。对我来说非常基本,这很好。好吧,我现在需要更进一步,在页脚中添加所有总行的SUM。
我怎样才能做到这一点?以下是每次更改employeeid时我用来添加总计的内容:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridView1.Columns[14].Visible = false;
if (e.Row.RowType == DataControlRowType.DataRow)
{
employeeid = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "employeeid").ToString());
decimal tmpfield1 = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "field1").ToString());
decimal tmpfield2 = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "field2").ToString());
decimal tmpfield3= Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "field3").ToString());
decimal tmpfield4 = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "field4”).ToString());
decimal tmpfield5 = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "field5").ToString());
decimal tmpfield6 = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "field6").ToString());
qtyfield1 += tmpfield1;
qtyfield2 += tmpfield2;
qtyfield3 += tmpfield3
qtyfield4+= tmpfield4;
qtyfield5+= tmpfield5;
qtyfield6 += tmpfield6;
}
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
bool newRow = false;
if ((employeeid > 0) && (DataBinder.Eval(e.Row.DataItem, "employeeid") != null))
{
if (employeeid != Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "employeeid").ToString()))
newRow = true;
}
if ((employeeid > 0) && (DataBinder.Eval(e.Row.DataItem, "employeeid") == null))
{
newRow = true;
rowIndex = 0;
}
if (newRow)
{
wh = “11”;
GridView GridView1 = (GridView)sender;
GridViewRow NewTotalRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
NewTotalRow.Font.Bold = true;
NewTotalRow.BackColor = System.Drawing.Color.Gray;
NewTotalRow.ForeColor = System.Drawing.Color.White;
TableCell HeaderCell = new TableCell();
HeaderCell.Text = "Total";
HeaderCell.HorizontalAlign = HorizontalAlign.Left;
HeaderCell.ColumnSpan = 4;
NewTotalRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.HorizontalAlign = HorizontalAlign.Right;
HeaderCell.Text = qtyfield1.ToString();
NewTotalRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.HorizontalAlign = HorizontalAlign.Right;
HeaderCell.Text = qtyfield2.ToString();
NewTotalRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.HorizontalAlign = HorizontalAlign.Right;
HeaderCell.Text = qtyfield3.ToString();
NewTotalRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.HorizontalAlign = HorizontalAlign.Center;
HeaderCell.Text = qtyfield4.ToString();
NewTotalRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.HorizontalAlign = HorizontalAlign.Center;
HeaderCell.Text = qtyfield5.ToString();
NewTotalRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.HorizontalAlign = HorizontalAlign.Center;
decimal grandtotalfield6
try { grandtotalfield6 = Convert.ToDecimal(qtyfield6) / Convert.ToDecimal(wh); }
catch { grandtotalfield6 = 0.00M; }
HeaderCell.Text = grandtotalfield6.ToString("P", CultureInfo.InvariantCulture);
NewTotalRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.HorizontalAlign = HorizontalAlign.Right;
HeaderCell.Text = "";
HeaderCell.ColumnSpan = 4;
NewTotalRow.Cells.Add(HeaderCell);
GridView1.Controls[0].Controls.AddAt(e.Row.RowIndex + rowIndex, NewTotalRow);
rowIndex++;
qtyTotal = 0;
qtyfield1 = 0;
qtyfield2 = 0;
qtyfield3 = 0;
qtyfield4 = 0;
qtyfield5 = 0;
qtyfield6 = 0;
}
}
修改
经过进一步的思考和分析,我认为我可以通过这样做来捕获总数,但是我怎么能把它转过来并且能够将这些信息写入页脚?
sumf1 += qtyfield1;
sumf2 += qtyfield2;
sumf3 += qtyfield3;
sumf4 += qtyfield4;
sumf5 += qtyfield5;
sumf6 += qtyfield6;
答案 0 :(得分:0)
为什么不使用GridView1.Rows.Count选项?
GridView1.FooterRow.Cells[2].Text = Convert.ToString(GridView1.Rows.Count);
如果我理解你很好,他会从x行开始"将这个数量保存在隐藏的字段中"
然后每次添加一行时从总行中减去它,结果将是为该人员添加的总行数,然后将该值设置为页脚单元格
答案 1 :(得分:0)
你快到了。在您的RowDataBound
代码中添加支票,以确定您是在查看DataRow
还是Footer
如何做到这一点就是这样(当然为所有数量添加)你想要显示,但应该是一个快速的文本更改,因为语法将保持不变)
这是你背后的C#代码
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Process like you do in your method
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lblSum1 = (Label)e.Row.FindControl("lblTotalqty");
lblSum1.Text = Sum1.ToString();
}
}
修改您的HTML以添加到footertemplate
并填充如此
<FooterTemplate>
<div style="text-align: right;">
<asp:Label ID="lblSum1" runat="server" />
</div>
</FooterTemplate>