简体   繁体   English

如何在if语句中使用for循环进行验证

[英]How to make validation with for loop inside if statement

Hello this below code that deduct product quantity from the stock based on product quantity in grid view with id = gridagree 您好,下面的代码根据id = gridagree的网格视图中的产品数量从库存中减去产品数量

the issue is i want to deduct the products quantity with category =1 and put validation to check that all products quantity in gridagree if its bigger than the quantity in stock or not and if one product quantity bigger than the quantity in stoack the code should not executed 问题是我想扣除类别= 1的产品数量,并进行验证以检查grid中的所有产品数量是否都大于库存数量,并且是否一个产品数量大于库存数量,则不应验证代码被执行

Category Id =gridagree.Rows[index].Cells[7].Text

Product Id = gridagree.Rows[index].Cells[3].Text)

Product Quantity in the grid =gridagree.Rows[index].Cells[5].Text

StoreClass s = new StoreClass();

if(//suggested code)
{

lblmsg.Text="Product quantity is bigger than current balance please change product quantity and try again"// here also can i determine to the user which product has the issue product with id=?
}

else
{
for (int index = 0; index < this.gridagree.Rows.Count; index++)
                    {
                        if (gridagree.Rows[index].Cells[7].Text == "1")
                        {
                            string qun = s.getprodqun(Convert.ToInt32(gridagree.Rows[index].Cells[3].Text));
                            s.subtract(Convert.ToInt32(gridagree.Rows[index].Cells[3].Text), Convert.ToInt32(qun) - Convert.ToInt32(gridagree.Rows[index].Cells[5].Text));
                        }

                    }
}

I am steering away from commenting on the code, and will just provide you with what I believe you're asking. 我避免对代码发表评论,只会为您提供我认为您要问的内容。 With how you have things coded in your sample, you need another loop... 关于如何在示例中编写代码,您需要另一个循环...

int categoryIdIndex = 7;
int productIdIndex = 3;
int quantityRequestedIndex = 5;

List<string> errors = new List<string>()
for (int index = 0; index < this.gridagree.Rows.Count; index++)
{
  Row row = gridagree.Rows[index];
  int categoryId = Convert.ToInt32(row.Cells[categoryIdIndex].Text);
  int productId = Convert.ToInt32(row.Cells[productIdIndex].Text);
  int quantityRequested = Convert.ToInt32(row.Cells[quantityRequestedIndex].Text);
  int availableQuantity = s.getprodqun(productId);

  if(quantityRequested > availableQuantity)
  {
    errors.Add(string.Format("Inventory contains insufficient items for product id {0} ", productId));
  }
}

if(errors.Count == 0)
{
  ... process the orders ...
}
else
{
  ... show the errors
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM