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

C# 如何在if语句中使用for循环进行验证,c#,asp.net,C#,Asp.net,您好,下面的代码根据id=gridagree的网格视图中的产品数量从库存中扣除产品数量 问题是我想扣除类别为1的产品数量,并进行验证,以检查Grid中的所有产品数量是否大于库存数量,如果一个产品数量大于stoack中的数量,则不应执行代码 Category Id =gridagree.Rows[index].Cells[7].Text Product Id = gridagree.Rows[index].Cells[3].Text) Product Quantity in the grid

您好,下面的代码根据id=gridagree的网格视图中的产品数量从库存中扣除产品数量

问题是我想扣除类别为1的产品数量,并进行验证,以检查Grid中的所有产品数量是否大于库存数量,如果一个产品数量大于stoack中的数量,则不应执行代码

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));
                        }

                    }
}
Category Id=gridagree.Rows[index].单元格[7].文本
产品Id=gridagree.Rows[index]。单元格[3]。文本)
网格中的产品数量=gridagree.Rows[index]。单元格[5]。文本
StoreClass s=新的StoreClass();
if(//建议代码)
{
lblmsg.Text=“产品数量大于当前余额,请更改产品数量并重试”//在此我还可以向用户确定哪个产品具有id为的问题产品?
}
其他的
{
for(int index=0;index
我不再对代码进行评论,我只会向您提供我认为您所要求的内容。根据您在示例中的编码方式,您需要另一个循环

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
}
int categoryIdIndex=7;
int productIdIndex=3;
int quantityRequestedIndex=5;
列表错误=新列表()
for(int index=0;indexAvailableEquality)
{
errors.Add(string.Format(“库存包含的产品id{0},productId)项不足”);
}
}
如果(errors.Count==0)
{
…处理订单。。。
}
其他的
{
…显示错误
}

如果(数量>存储)
?我有更多的产品在网格中,我想检查所有的产品,所以您需要它在您的循环中?这个问题令人困惑。说清楚点。