Gridview的列和行总计 [英] Gridview column and row total
问题描述
我的gridview的数据如下所示(例如)
my gridview has data which looks like below (for example)
col1 col2 col3
2 10 1
4 3 5
11 15 18
我想做的是。 ..按行和列进行求和,然后将另一列添加到网格中……这样最终看起来像下面的
What I am trying to do is ... performing a sum by Row and Column and add another column to grid ... So that ultimately it will looks like below
col1 col2 col3 Total
2 10 1 13
4 3 5 12
11 15 18 44
17 28 24
在C#中是否适用。
是这样吗,我必须按行和列对网格进行解析,然后执行SUM。如下所示
is it like, I have to parse through the grid by Row and Column and then perform the SUM; like below
foreach (gridviewrow row in gridview1.rows)
{
add the value for cell 0;
cell 1;
cell 2;
}
是否有更好的wau来实现这一目标?
Is there any better wau to achive this? Thanks a lot.
谢谢,
Rahul
Thanks, Rahul
推荐答案
好了,我终于有了它,于是,想到了发布它……如果对其他人有帮助。
Well I have got it finally and So, thought of posting it ... if it helps others.
而不是处理来自网格的数据...我把在处理中求和的逻辑,然后将数据绑定到网格...起作用了。
Instead of processing the data from grid ... I put the logic for Sum in my processing and then bind that data to grid ... which worked.
也可以在gridview中完成...我们必须将处理在以下 gridview_onrowdatabound事件中
Can be done in gridview as well ... we have to put the processing in "gridview_onrowdatabound" event as below
protected void gridview1_onrowdatabound(Object sender, GridViewRowEventArgs e)
{
// Check if it's a header row
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells.add("Total"); //add a header Col
}
int count = 0;
if(e.Row.RowType == DataControlRowType.DataRow)
{
count = count + Convert.ToInt32(e.Row.Cells[0].value)+cell1 value+cell2 value;
e.Row.Cells.add(count.tostring());
}
}
希望有帮助。
谢谢,
拉胡尔
Hope this Helps. Thanks, Rahul
这篇关于Gridview的列和行总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!