我有代码,一切都很好,直到我输入大于3000 的数字,结果将是负数,请帮忙。
我不知道为什么会这样,我已经调试过了,或者我用int声明了它?
我提到的总价值是 int total=数量*成本;
顺便说一句,有没有办法阻止用户在数量列中输入除数字之外的任何其他内容?
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;
}
}