计算总值是错误的

时间:2017-03-14 07:22:23

标签: c# datagridview calculation

我有代码,一切都很好,直到我把数字大于 3000,结果 负数,请帮忙。

我不知道为什么会发生这种情况,我已经调试过,或者我是否通过int声明它?

我提到的总价值是 int total = quantity * cost;

顺便说一句,无论如何阻止用户输入任何除了数字数量列?

 private void G2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        try
        {
            int quantity, cost;
            if (int.TryParse(G2.Rows[e.RowIndex].Cells["Quantity"].Value.ToString(), out quantity) && int.TryParse(G2.Rows[e.RowIndex].Cells["Cost2"].Value.ToString(), out cost))
            {
                int total= quantity * cost;
                G2.Rows[e.RowIndex].Cells["Total"].Value = total.ToString();
            }

            int quan, mini;

            quan = Convert.ToInt32(G2.Rows[e.RowIndex].Cells["Quantity"].Value);
            mini = Convert.ToInt32(G2.Rows[e.RowIndex].Cells["MinimumOrder2"].Value);

            if (quan < mini)
            {
                MessageBox.Show("QUANTITY must be GREATER or EQUAL to MINIMUM ORDER", "STOP", MessageBoxButtons.OK, MessageBoxIcon.Error);
                G2.Rows[e.RowIndex].Cells["Quantity"].Value = "";
                G2.Rows[e.RowIndex].Cells["Total"].Value = "";
                return;

            }
            else
            {
                //Sum the Total Column to TOTAL VALUES text box
                decimal TotalValue = 0;
                for (int i = 0; i < G2.Rows.Count; i++)
                {
                    if (G2.Rows[i].Cells["Total"].Value == DBNull.Value)
                    {
                        return;
                    }
                    else
                    {
                        TotalValue += Convert.ToDecimal(G2.Rows[i].Cells["Total"].Value);
                    }
                }

                totalvalue.Text = TotalValue.ToString();
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show("Please Enter Only Number");
            return;
        }

    }

Example input by user

3 个答案:

答案 0 :(得分:1)

[已解决]不要再使用int,将其更改为十进制

private void G2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    try
    {
        decimal quantity, cost;
        if (decimal .TryParse(G2.Rows[e.RowIndex].Cells["Quantity"].Value.ToString(), out quantity) && decimal .TryParse(G2.Rows[e.RowIndex].Cells["Cost2"].Value.ToString(), out cost))
        {
            decimal total= quantity * cost;
            G2.Rows[e.RowIndex].Cells["Total"].Value = total.ToString();
        }

        int quan, mini;

        quan = Convert.ToInt32(G2.Rows[e.RowIndex].Cells["Quantity"].Value);
        mini = Convert.ToInt32(G2.Rows[e.RowIndex].Cells["MinimumOrder2"].Value);

        if (quan < mini)
        {
            MessageBox.Show("QUANTITY must be GREATER or EQUAL to MINIMUM ORDER", "STOP", MessageBoxButtons.OK, MessageBoxIcon.Error);
            G2.Rows[e.RowIndex].Cells["Quantity"].Value = "";
            G2.Rows[e.RowIndex].Cells["Total"].Value = "";
            return;

        }
        else
        {
            //Sum the Total Column to TOTAL VALUES text box
            decimal TotalValue = 0;
            for (int i = 0; i < G2.Rows.Count; i++)
            {
                if (G2.Rows[i].Cells["Total"].Value == DBNull.Value)
                {
                    return;
                }
                else
                {
                    TotalValue += Convert.ToDecimal(G2.Rows[i].Cells["Total"].Value);
                }
            }

            totalvalue.Text = TotalValue.ToString();
        }

    }
    catch (Exception ex)
    {
        MessageBox.Show("Please Enter Only Number");
        return;
    }

}

答案 1 :(得分:0)

一个问题是“总循环”。我认为return应该是continue

if (G2.Rows[i].Cells["Total"].Value == DBNull.Value)
{
    continue;
}

另一个问题是,点击行的计算total计算为int而不是decimial。将代码更改为:

int quantity;
decimal cost;
if(int.TryParse(G2.Rows[e.RowIndex].Cells["Quantity"].Value.ToString(), out quantity) && 
   decimal.TryParse(G2.Rows[e.RowIndex].Cells["Cost2"].Value.ToString(), out cost))
        {
            int total= quantity * cost;
            G2.Rows[e.RowIndex].Cells["Total"].Value = total.ToString();
        }

答案 2 :(得分:0)

另一个问题可能是允许用户输入Total中的值。并且以下是仅允许数量字段

中的数字的代码
private void G2_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        String sCellName = G2.Columns[G2.CurrentCell.ColumnIndex].Name.ToUpper();
        if (sCellName == "QUANTITY") //----change with yours
        {

            e.Control.KeyPress +=  new KeyPressEventHandler(Control_KeyPress);


        }
    }

    private void Control_KeyPress(object sender, KeyPressEventArgs e)
    {

        if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
        {
            e.Handled = true;
        }

        // only allow one decimal point
        if (e.KeyChar == '.'
            && (sender as TextBox).Text.IndexOf('.') > -1)
        {
            e.Handled = true;
        }
    }