【问题标题】:Calculating Sum on similar cell value in datagridview在datagridview中计算相似单元格值的总和
【发布时间】:2013-09-13 06:41:57
【问题描述】:

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

如果部门是第一行的架构,其TMH 单元格在datagridview 下方的值为36。

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

然后我又选择了第三行的建筑系,它的 TMH 值现在是 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。

我想要这种情况的逻辑。

【问题讨论】:

  • 请添加您已经尝试过的代码。带有值的示例设置也将非常有帮助。
  • 问题仍未解决,有人来帮忙;
  • 你在哪里定义m_Project
  • m_project 是我的实体上下文...我从中访问表对象。
  • 那么,如果我要重申您的问题,您想在您的上下文中查询projdepthrs 表中所有与部门相同的部门,并对TMH 列中的值求和?跨度>

标签: c# mysql datagridview


【解决方案1】:

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

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

如果我理解有误,请评论。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多