从datagridview获得总价值 [英] get total value from a datagridview

查看:92
本文介绍了从datagridview获得总价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的会员,
我有问题.
在我的datagridview列金额"中,我想要该颜色中所有值的总和.
但是主要的问题是,假设有3行,并且值是100,200,400,我的光标在400列中,那么总值是300.

意思是当列值改变时我想要值,但是我在那个列中的光标..


谢谢.

解决方案

  private   十进制 [ 31 ];

 for ( int  count =  0 ; count <  myVariable; count ++)
{
     d [count] = Convert.ToDecimal(myGrid.Rows [e.RowIndex] .Cells [count] .Value);
}

myGrid.Rows [e.RowIndex] .Cells.Value = d [ 0 ] + d [ 29 ] ;例如,// .

我在 CellEndEdit 事件中添加了  /pre> 

或尝试一下

 十进制总计= 0M;
 foreach (DataGridViewRow行 in  datagridview1.Rows中)
{
      十进制 temp = 0M;
      如果(Decimal.TryParse(dataGridView [ 0 ,row.Index] .Value.ToString(),输出临时))//  0是第一列-设置您的列索引!
           总+ =温度;
}
// 在最后一行中设置总计的结果:
datagridview1 [ 0 ,datagridview1.Rows.Count -1] .Value = total.ToString(); 



或查看此链接

http://www.timvw.be/2007/01/04 /developing-a-datasource-for-your-datagridview/ [ Windows窗体中的DataGridView –提示,技巧和常见问题解答(FAQ) [ ^ ].


Dear Member,
i have a problem.
in my datagridview a column "amount" i want total of sum of all values in that coluns.
but main problem is that suppose there is 3 rows and there values are 100,200,400 and my curser in 400 columns then total values is 300.

Means i want values when columns values change but my curser in that columns..


Thans you.

解决方案

private decimal[] d;
d = new decimal[31];

for(int count = 0; count < myVariable; count++)
{
     d[count] = Convert.ToDecimal(myGrid.Rows[e.RowIndex].Cells[count].Value);
}

myGrid.Rows[e.RowIndex].Cells.Value = d[0] + d[29]; //for example.

I added this in CellEndEdit event.



Or Try This

decimal total = 0M;
foreach(DataGridViewRow row in datagridview1.Rows)
{
      decimal temp = 0M;
      if(Decimal.TryParse(dataGridView[0, row.Index].Value.ToString(), out temp)) // 0 is 1st column - set your column index!
           total += temp;
}
//set result of total in last row:
datagridview1[0, datagridview1.Rows.Count -1].Value = total.ToString();



Or See This Link

http://www.timvw.be/2007/01/04/developing-a-datasource-for-your-datagridview/[^]

Accept This Answer If It Has Helped You


Have a look at Tip 7 at DataGridView in Windows Forms – Tips, Tricks and Frequently Asked Questions(FAQ)[^].


这篇关于从datagridview获得总价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆