C# 计算重要性A是坏的

C# 计算重要性A是坏的,c#,C#,在我的DataGrid中,我从不同的文本框加载数据。金额经过完美计算,问题在于IVA的金额。通过将其放在下面的标签中,它是正确的,但再次单击按钮,IVA计算结果不正确。我已经设置了断点,他捕捉到的数字是正确的,但是乘法对我来说是不好的,我不知道会发生什么,因为在正常数量下,他做的是正确的。假设IVA是12100,当你第一次点击它时,它给了我24200,但是当你再次点击时,它给了我大约98000,当它必须是一半时,大约48000,我不明白为什么我做了错误的事情。我附上代码的完整片段 private

在我的DataGrid中,我从不同的文本框加载数据。金额经过完美计算,问题在于IVA的金额。通过将其放在下面的标签中,它是正确的,但再次单击按钮,IVA计算结果不正确。我已经设置了断点,他捕捉到的数字是正确的,但是乘法对我来说是不好的,我不知道会发生什么,因为在正常数量下,他做的是正确的。假设IVA是12100,当你第一次点击它时,它给了我24200,但是当你再次点击时,它给了我大约98000,当它必须是一半时,大约48000,我不明白为什么我做了错误的事情。我附上代码的完整片段

private void btnColocar_Click(object sender, EventArgs e)
    {
        if (Utilidades.ValidarFormulario(this, errorProvider1) == false)
        {
            bool existe = false;
            int num_fila = 0;

            if (cont_fila == 0)
            {
                dataGridView1.Rows.Add(txtCodigoPro.Text, txtDescripcion.Text, txtPrecio.Text, txtCantidad.Text, txtIVA.Text);
                double importe = Convert.ToDouble(dataGridView1.Rows[cont_fila].Cells[2].Value) * Convert.ToDouble(dataGridView1.Rows[cont_fila].Cells[3].Value);
                double importeIVA = Convert.ToDouble(dataGridView1.Rows[cont_fila].Cells[4].Value) * Convert.ToDouble(dataGridView1.Rows[cont_fila].Cells[3].Value);
                dataGridView1.Rows[cont_fila].Cells[4].Value = importe;
                dataGridView1.Rows[cont_fila].Cells[5].Value = importeIVA;

                cont_fila++;

            }

            else
            {
                foreach (DataGridViewRow Fila in dataGridView1.Rows)
                {
                    if (Fila.Cells[0].Value.ToString() == txtCodigoPro.Text)
                    {
                        existe = true;
                        num_fila = Fila.Index;
                    }
                }

                if (existe == true)
                {
                    dataGridView1.Rows[num_fila].Cells[3].Value = (Convert.ToDouble(txtCantidad.Text) + Convert.ToDouble(dataGridView1.Rows[num_fila].Cells[3].Value)).ToString();
                    double importe = Convert.ToDouble(dataGridView1.Rows[num_fila].Cells[2].Value) * Convert.ToDouble(dataGridView1.Rows[num_fila].Cells[3].Value);

                    dataGridView1.Rows[num_fila].Cells[4].Value = importe;

                    double importeIVA = Convert.ToDouble(dataGridView1.Rows[num_fila].Cells[5].Value) * Convert.ToDouble(dataGridView1.Rows[num_fila].Cells[3].Value);
                   // double importeIVA = Convert.ToDouble(dataGridView1.Rows[num_fila].Cells[3].Value) * Convert.ToDouble(dataGridView1.Rows[num_fila].Cells[6].Value);
                    // 3
                    dataGridView1.Rows[num_fila].Cells[5].Value = importeIVA;
                }
                else
                {
                    dataGridView1.Rows.Add(txtCodigoPro.Text, txtDescripcion.Text, txtPrecio.Text, txtCantidad.Text, txtIVA.Text);
                    double importe = Convert.ToDouble(dataGridView1.Rows[cont_fila].Cells[2].Value) * Convert.ToDouble(dataGridView1.Rows[cont_fila].Cells[3].Value);
                    dataGridView1.Rows[cont_fila].Cells[4].Value = importe;

                    double importeIVA = Convert.ToDouble(dataGridView1.Rows[cont_fila].Cells[4].Value) * Convert.ToDouble(dataGridView1.Rows[cont_fila].Cells[3].Value);
                    dataGridView1.Rows[cont_fila].Cells[5].Value = importeIVA;

                cont_fila++;
                }
            }

            total = 0;
            totalIVA = 0;
            foreach (DataGridViewRow Fila in dataGridView1.Rows)
            {
                total += Convert.ToDouble(Fila.Cells[4].Value); 
                totalIVA += Convert.ToDouble(Fila.Cells[5].Value);
            }


           lblTotal.Text = total.ToString() +  " €";
           lblIVA.Text = totalIVA.ToString() + " €";


        }

    }
代码在此崩溃

foreach (DataGridViewRow Fila in dataGridView1.Rows)
                {
                    if (Fila.Cells[0].Value.ToString() == txtCodigoPro.Text)
                    {
                        existe = true;
                        num_fila = Fila.Index;
                    }
                }

                if (existe == true)
                {
                    dataGridView1.Rows[num_fila].Cells[3].Value = (Convert.ToDouble(txtCantidad.Text) + Convert.ToDouble(dataGridView1.Rows[num_fila].Cells[3].Value)).ToString();
                    double importe = Convert.ToDouble(dataGridView1.Rows[num_fila].Cells[2].Value) * Convert.ToDouble(dataGridView1.Rows[num_fila].Cells[3].Value);

                    dataGridView1.Rows[num_fila].Cells[4].Value = importe;
///在我身上有多种多样性

double importeIVA = Convert.ToDouble(dataGridView1.Rows[num_fila].Cells[5].Value) * Convert.ToDouble(dataGridView1.Rows[num_fila].Cells[3].Value);

                        dataGridView1.Rows[num_fila].Cells[5].Value = importeIVA;

Thanxx

如果记录不存在,您可以通过将金额
单元格[3]
乘以
导入(
单元格[4]
)来计算IVA。 但是如果它存在的话

double importeIVA = 
    Convert.ToDouble(dataGridView1.Rows[num_fila].Cells[5].Value) *
    Convert.ToDouble(dataGridView1.Rows[num_fila].Cells[3].Value);
将金额(
单元格[3]
)乘以先前计算的IVA(
单元格[5]
),而不是
导入的值

所以我认为应该是这样

double importeIVA = 
    Convert.ToDouble(dataGridView1.Rows[num_fila].Cells[4].Value) *
    Convert.ToDouble(dataGridView1.Rows[num_fila].Cells[3].Value);