我想知道如何在gridview中显示各列的总和?
问题描述:
我在gridview中有6列(salary,ta,contingency,nrc,institcharges,others)我想要的是我想得到每列页脚中每列的总和。为此,我已经采取了模板字段并在其中插入了一个标签,但我的问题在于我的编码结果我得到的是每个标签上所有列的总和
这是我在我的代码后面使用的代码:
i have 6 columns (salary,ta,contingency,nrc,institcharges,others) in my gridview what i wanna do is i want to get the sum of each column in the footer of each column. for this i have taken template fields and inserted a label in them but my problem is in my coding the result i am getting is total of all the columns on each label
this is the code i m using in my code behind file:
int total = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "salary"));
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label sallabel = (Label)e.Row.FindControl("sallabel");
sallabel.Text = total.ToString();
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ta"));
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lblamount2 = (Label)e.Row.FindControl("talabel");
lblamount2.Text = total.ToString();
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "contigency"));
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lblamount3 = (Label)e.Row.FindControl("cnlabel");
lblamount3.Text = total.ToString();
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "nrc"));
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lblamount4 = (Label)e.Row.FindControl("nrclabel");
lblamount4.Text = total.ToString();
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "institcharges"));
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lblamount5 = (Label)e.Row.FindControl("iclabel");
lblamount5.Text = total.ToString();
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "others"));
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lblamount6 = (Label)e.Row.FindControl("olabel");
lblamount6.Text = total.ToString();
}
}
答
http://msdn.microsoft.com/en-us/library/ms972833.aspx [ ^ ]
http://*.com/questions/12853406/how-to-diplay-boundfield-columns-values -sum-in-footer-row [ ^ ]
http://aspalliance.com/ 782_CodeSnip_How_to_Display_Sum_Total_in_the_Footer_of_the_GridView_Control [ ^ ]
http://techbrij.com/831/display-total-gridview-footer-row-linq-asp-net [ ^ ]
http://msdn.microsoft.com/en-us/library/ms972833.aspx[^]
http://*.com/questions/12853406/how-to-diplay-boundfield-columns-values-sum-in-footer-row[^]
http://aspalliance.com/782_CodeSnip_How_to_Display_Sum_Total_in_the_Footer_of_the_GridView_Control[^]
http://techbrij.com/831/display-total-gridview-footer-row-linq-asp-net[^]