【发布时间】:2014-11-27 04:30:34
【问题描述】:
在asp.net C#中,如何将gridview中列的总和获取到gridview外部的文本框中?
注意:Gridview 数据并非来自用户手动输入的任何数据库。
【问题讨论】:
标签: c# asp.net gridview datagridviewcolumn
在asp.net C#中,如何将gridview中列的总和获取到gridview外部的文本框中?
注意:Gridview 数据并非来自用户手动输入的任何数据库。
【问题讨论】:
标签: c# asp.net gridview datagridviewcolumn
在单击某个按钮或创建新行或您想要的任何事件时调用此方法
private void GrandTotal()
{
float GTotal = 0f;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
String total = (GridView1.Rows[i].FindControl("lblTotal") as Label).Text;
GTotal += Convert.ToSingle(total);
}
txtTotal.text=GTotal.toString();
}
希望我能帮上忙:D
【讨论】:
Object reference not set to an instance of an object.为什么?
试试这个
int sum = 0;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
sum +=Convert.ToInt32( GridView1.Rows[i].Cells[/*the column index*/].Text);
}
【讨论】:
这应该可以完成工作。
int total = 0;
for (int row = 0; row < GridView1.Rows.Count; row ++)
{
for (int col = 0; col < GridView1.Columns.Count; col++)
total +=Convert.ToInt32( GridView1.Rows[row].Cells[col].Text);
}
【讨论】: