怎么样给grideview中的某一列求和

gylc168 2010-11-01 02:11:14
grideview从数据库读取数据,行数是不固定的,我求某一列的和,比如这一列是“Menoy”,该怎么做呢?
另外我想找到grideview中的某个特定的单元格,比如从左到右,从上到下,最后一列与最后一行的交叉的那个单元格,该怎么做呢?
...全文
133 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
laomengzi 2010-11-01
  • 打赏
  • 举报
回复
学习!
cpp2017 2010-11-01
  • 打赏
  • 举报
回复
不能动下脑筋么?
cpp2017 2010-11-01
  • 打赏
  • 举报
回复
Rows.Count-1


Rows[0].Cells.Count-1
gylc168 2010-11-01
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 cpp2017 的回复:]
1。Object o = dt.Compute("SUM(字段)","");
2. GridView1.Rows[n].Cells[n]
[/Quote]

关键是行数不固定啊,你这N没用的
a12321321321312321 2010-11-01
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jxyxhz 的回复:]
1楼这么好的例子这么不看下呢。
[/Quote]
itnodezhangbing 2010-11-01
  • 打赏
  • 举报
回复
不好意思,打错字了。
itnodezhangbing 2010-11-01
  • 打赏
  • 举报
回复
循环狗日的girdview每一行,把那个money列的值累加
边城的刀声 2010-11-01
  • 打赏
  • 举报
回复

/// <summary>
/// 数据源为DataTable的GridView列汇总器
/// </summary>
public class GridViewTotaller
{
#region Variables
private GridView gridView;
public string DispHTMLFormat = "{0}";

#endregion

#region Constructor
public GridViewTotaller(GridView gridView)
{
this.gridView = gridView;
}
#endregion

/// <summary>
/// 汇总Grid某一列的数据
/// </summary>
/// <param name="totalFld">要统计的列名</param>
/// <param name="cellIndex">要统计列的index</param>
/// <param name="format">格式,默认值为空</param>
public void Total<T>(String totalFld, Int32 cellIndex, String format) where T : struct, IFormattable
{
DataTable dt = (DataTable)gridView.DataSource;
if (dt.Rows.Count <= 0) return;
Object oValue = dt.Compute(String.Format("sum({0})", totalFld), "");
T value = (T)(oValue == DBNull.Value ? 0 : Convert.ChangeType(oValue, typeof(T)));
gridView.ShowFooter = true;
gridView.FooterRow.HorizontalAlign = HorizontalAlign.Right;
gridView.FooterRow.Cells[cellIndex].Text += String.Format(DispHTMLFormat, String.Format("{0}", value.ToString(format, null)));
}

public void Total<T>(String totalFld, Int32 cellIndex) where T : struct, IFormattable
{
Total<T>(totalFld, cellIndex, null);
}

/// <summary>
/// 汇总Grid某一列的数据
/// </summary>
/// <param name="grd">gridView</param>
/// <param name="totalFld">要汇总的字段</param>
/// <param name="cellIndex">要显示的单元格索引</param>
public static void TotalInfo(GridView grd, String totalFld, Int32 cellIndex)
{
GridViewTotaller totaller = new GridViewTotaller(grd);
totaller.Total<Decimal>(totalFld, cellIndex, "N2");
}
}
小_虎 2010-11-01
  • 打赏
  • 举报
回复
1楼这么好的例子这么不看下呢。
cpp2017 2010-11-01
  • 打赏
  • 举报
回复
1。Object o = dt.Compute("SUM(字段)","");
2. GridView1.Rows[n].Cells[n]

gylc168 2010-11-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 cpp2017 的回复:]
1.可以用sql做sum
2. 可以用DataTable的compute计算. Object o = dt.Compute("SUM(字段)","");

3. 循环各个gridviewrow,累加计算
[/Quote]

能具体一点吗?
cpp2017 2010-11-01
  • 打赏
  • 举报
回复
1.可以用sql做sum
2. 可以用DataTable的compute计算. Object o = dt.Compute("SUM(字段)","");

3. 循环各个gridviewrow,累加计算
小_虎 2010-11-01
  • 打赏
  • 举报
回复

62,039

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧