在类似的datagridview单元格的值计算总和 [英] Calculating Sum on similar cell value in datagridview

查看:145
本文介绍了在类似的datagridview单元格的值计算总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,在这个的datagridview ,我想caculate总和类似的部门。

Here, in this datagridview, i want to caculate sum for similar departments.

也就是说,如果部门在第一排,并在下面及其 TMH 细胞datagridview的架构值为36.

That is if department is architecture on 1st row and its TMH cell in below datagridview value is 36.

,然后在第二排我的部门是设计和TMH单元格的值是45。

And then on 2nd row my department is design and its TMH cell value is 45.

和上第三行和TMH值,有一次我选择了建筑系,现在是45。

And then again i have selected architecture department on 3rd row and its TMH value is now 45.

下面是我的代码

           private void ProjectActivitiesGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        Decimal sum = 0;

        #region THM CALCULATION.

        int column = ProjectActivitiesGrid.CurrentCell.ColumnIndex;
        string headerText = ProjectActivitiesGrid.Columns[column].HeaderText;

        DataGridViewRow d = this.ProjectActivitiesGrid.Rows[e.RowIndex];

        String department = d.Cells[0].Value.ToString();

            String chk = m_Project.projdepthrs.Where(c => c.DEPARTMENT == department).Select(c => c.DEPARTMENT).FirstOrDefault();

            if (chk == null)
            {
                MessageBox.Show("Please fill up department alloted hrs first");
                d.Cells[1].ReadOnly = true;
                d.Cells[2].ReadOnly = true;
                d.Cells[3].ReadOnly = true;
                d.Cells[4].ReadOnly = true;
                d.Cells[5].ReadOnly = true;
                d.Cells[6].ReadOnly = true;
                d.Cells[7].ReadOnly = true;

            }
            else
            {
                d.Cells[1].ReadOnly = false;
                d.Cells[2].ReadOnly = false;
                d.Cells[3].ReadOnly = false;
                d.Cells[4].ReadOnly = false;
                d.Cells[5].ReadOnly = false;
                d.Cells[6].ReadOnly = false;
                d.Cells[7].ReadOnly = false;

                String tmh = m_Project.projdepthrs.Where(c => c.DEPARTMENT == department).Select(c => c.TMH).First();
                LBLAllotedhrs.Text = tmh;
                LBLLefthrs.Text = tmh;
            }


               foreach (DataGridViewRow rw in ProjectActivitiesGrid.Rows)
               {
                       if (department == d.Cells[0].Value.ToString())
                           sum = sum + Convert.ToInt32(d.Cells[5].Value);
                       else
                           sum = 0;

               }
                LBLLefthrs.Text = (Convert.ToInt32(LBLLefthrs.Text) - Convert.ToInt32(sum)).ToString(); 

        #endregion


    }

然后,如果我能得到我的总和为36 + 45的第1和第3行有异曲同工之处下拉列表。

Then if how can i get my sum as 36+45 for 1st and 3rd row having similar department dropdown list.

我想这种情况的逻辑。

推荐答案

所以,如果我理解正确,你想要的东西,如:

So if I understand correctly, you want something like:

var result = m_Project.projdepthrs.Where(md=> md.DEPARTMENT == department)
                                    .Sum(ms=> Convert.ToInt32(ms.TMH));

请评论,如果我有误解。

Please comment if I have misunderstood.

这篇关于在类似的datagridview单元格的值计算总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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