如果单元格包含一些符号,则对多个数字求和

sum multiple digits if cell contains some symbols

我有一个数据网格视图,我在其中计算列中的值并乘以另一个列值,然后对所有行求和并使用以下代码在标签中显示结果:

int sum2 = 0;
            for (int m = 0; m < dataGridView1.Rows.Count; ++m)
            {
                sum2 += getCellRepsSum(dataGridView1.Rows[m]);
            }
     
        label18.Text = sum2.ToString();


       

        int getCellRepsSum(DataGridViewRow dr)
        {
            int serie = Convert.ToInt32(dr.Cells[1].Value);


            int l = Convert.ToInt32(dr.Cells[2].Value); //reps


            int result = 0;



            result = serie * l;
            return result;
        }

如果我设置一个数字,这很好用,但我想根据 + 或 - 等符号的存在添加条件。

类似...如果“ripetizioni”列的单元格值包含“-”或“+”,则对每个符号之间的数字求和,然后像我的代码一样相乘。

示例:12-8-6 需要对 12 + 8 + 6 求和,然后乘以“serie”列,就像在我的代码等中一样

我试过这样但没有用,因为“-”符号不允许 ToInt 转换:

int sum2 = 0;
            for (int m = 0; m < dataGridView1.Rows.Count; ++m)
            {
                sum2 += getCellRepsSum(dataGridView1.Rows[m]);
            }
     
        label18.Text = sum2.ToString();


       

        int getCellRepsSum(DataGridViewRow dr)
        {
            int serie = Convert.ToInt32(dr.Cells[1].Value);

            int result = 0;
            string l = Convert.ToString(dr.Cells[2].Value);

            if (l.Contains("-")) {

                int lt = Convert.ToInt32(dr.Cells[2].Value);
                int sumReps = 0;

                

                while (lt > 0)
                {
                    sumReps += (lt % 10);

                    lt = lt / 10;
                }
                result = sumReps;
                return result;

            }
            else {
                //reps

                int li = Convert.ToInt32(dr.Cells[2].Value); //reps




                result = serie * li;
                return result;
            }
            
            return result;
        }

我该如何解决这个问题?

改变

int lt = Convert.ToInt32(dr.Cells[2].Value);

int lt = dr.Cells[2].Value.ToString().Split('+','-').Select(int.Parse).Sum();

别忘了乘以 serie