gpt4 book ai didi

c# - 计算datagridview中相似单元格值的总和

转载 作者:行者123 更新时间:2023-11-29 00:26:24 25 4
gpt4 key购买 nike

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

也就是说,如果部门是第 1 行的体系结构,并且其 datagridview 下方的 TMH 单元格值为 36。

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

然后我再次选择了第 3 行的建筑部门,它的 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


}

那么对于具有类似部门下拉列表的第 1 行和第 3 行,我如何得到 36+45 的总和。

我想要这种情况的逻辑。

最佳答案

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

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

如果理解有误,欢迎评论。

关于c# - 计算datagridview中相似单元格值的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18703943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com