从数据网格求和 [英] summing from a datagrid

查看:130
本文介绍了从数据网格求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是问题所在.我有一张桌子,我想要做的是求和,但仅适用于该桌子中的一个人,而不是所有的人.等



ID名姓家庭名金额

-------------------------------------------------- ----------------------------

1约翰·史密斯500

1约翰·史密斯250

2艾莉森·怀特1000

2

3安德鲁·卡赞斯基300


Hy,

here''s the problem. I got a table and what I want to do is to make a sum, but only for a single person in that table, not all of them. etc.

Table

ID FirstName FamilyName Amount

------------------------------------------------------------------------------

1 John Smith 500

1 John Smith 250

2 Alison White 1000

2

3 Andrew Kazinsky 300


 decimal zbroj = 0;
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
    DataGridViewCell cell = row.Cells[5];
    {
        if (cell.Value != null)
        {
            zbroj += Convert.ToDecimal(cell.Value);
        }
    }
}
tbIznosBrutoDodatkaOdbitka.Text = zbroj.ToString();



我得到2050的总和,而我需要的是750(仅适用于约翰·史密斯).

如果我还不够清楚,我不仅需要约翰·史密斯的总和,而且不需要所有人的总和,即使该人被放在多行中也是如此.

寻求帮助.



I get a sum of 2050, and what I need is to get 750 (only for John Smith).

If I haven''t make myself clear enough, I do not need sum only for John Smith, but for everyone, even if that person is placed in multiple rows.

Thx for any help.

推荐答案

decimal zbroj = 0;
            foreach (DataGridViewRow row in this.dataGridView1.Rows)
            {
                DataGridViewCell cell = row.Cells[5];
                {
                    if (cell.Value != null && row.Cells[4].Value=="John Smith")
                    {
                        zbroj += Convert.ToDecimal(cell.Value);                       
                    }
                }
            }
            tbIznosBrutoDodatkaOdbitka.Text = zbroj.ToString();



更新了代码.请检查.



Updated the code..please check..


您可以在上面的循环中编写一些逻辑来做到这一点(例如,将名称存储在字典中,然后对照键进行检查).如果键存在,则将其与字典的值相加.

但是,在数据库上运行分组依据和总和"查询会更容易.
You can write some logic in your loop above to do that (for e.g. store the name in a dictionary and then check against the key). If the key exists, total against the value of the dictionary.

However, it would just be easier to run a Group By and Sum query on the database.


获取网格列的总和...

to get sum of column of a grid ...

double total_amt = 0;
              for (int r = 0; r < grd_bill_master.Rows.Count - 1; r++)
              {
                  //if (!Convert.ToDouble(grd_bill_master.Rows[r].Cells[15].Value))
                  //{
                  total_amt = total_amt + Convert.ToDouble(grd_bill_master.Rows[r].Cells[15].Value);
                  //}
              }
              txt_total_amount.Text = total_amt.ToString();


这篇关于从数据网格求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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