【问题标题】:Multiply two column datagridview cell values for another column then sum values of all rows将另一列的两列datagridview单元格值相乘,然后将所有行的值相加
【发布时间】:2021-07-13 15:00:20
【问题描述】:

我有这个datagridview,我用这段代码总结了所有时间的紧张值:

    int finalSum = 0;
for (int p = 0; p < dataGridView1.Rows.Count; ++p)
  {
     finalSum += getCellDigitSum(Convert.ToInt32(dataGridView1.Rows[p].Cells[4].Value));
  }
    
 label22.Text = finalSum.ToString();
    
 int getCellDigitSum(int cellValue)
   {
     int l = cellValue;
     int result = 0;
    
     while (l > 0)
       {
         result += (l % 10);
    
         l = l / 10;
       }
    
    return result;
 }

使用此代码,我获得单个值的总和(201 变为 3),然后是所有行的总和。

问题是我需要先将“Ripetizioni”值乘以“Serie”列值,然后将结果乘以“压力下的时间”(201 将是 3)的多个数字的总和,最后将所有行的结果相乘。

这是数据网格视图

所以例如在第一行我需要做 11 x 3 = result x (201 sum so 3)。

循环所有行并求和

【问题讨论】:

    标签: c# datagridview sum


    【解决方案1】:

    请试试这个。如果您遇到任何问题,请告诉我。计算可根据需要修改。

            int finalSum = 0;
            for (int p = 0; p < dataGridView1.Rows.Count; ++p)
            {
                finalSum += getCellDigitSum(dataGridView1.Rows[p]);
            }
    
            //label22.Text = finalSum.ToString();
    
            int getCellDigitSum(DataGridViewRow dr)
            {
                int serie = Convert.ToInt32(dr.Cells[0].Value);//you can change cell index or use column name
                int ripetizioni = Convert.ToInt32(dr.Cells[1].Value);//you can change cell index or use column name
                //timeundertension
                int l = Convert.ToInt32(dr.Cells[3].Value);
                int sumTimeUnderTension = 0;
    
                int result = 0;
    
                while (l > 0)
                {
                    sumTimeUnderTension += (l % 10);
    
                    l = l / 10;
                }
    
                result = serie * ripetizioni * sumTimeUnderTension;
                return result;
            }
    

    【讨论】:

    • 是的,我只删除了 label22.Text 之前的“//”并且有效!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2018-02-10
    • 2020-08-19
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    相关资源
    最近更新 更多