将方法从一种方法传递到另一种方法时的逻辑

时间:2014-01-23 13:33:30

标签: c# asp.net gridview

我在使用c#进行编码的逻辑上遇到了一些问题。我有一个列表,用于存储标准包装单元中的所有物品。当我循环通过列表时,如果我检测到某个项目库存不足,我会得到该项目的类别。之后,我传递给一个方法,该方法按降序对某个类别中的项目进行排序。然后我通过降序列表循环以获得具有最高库存的顶级产品,我将在网格视图中显示该项目作为建议项目。以下是我如何设置建议的网格视图:

<asp:GridView ID="gvSuggested" runat="server" AutoGenerateColumns="False" CellPadding="2" ForeColor="#333333" GridLines="None" Width="300px">
                            <Columns>
                                <asp:BoundField DataField="name" HeaderText="Product Name" ItemStyle-Width="100px" />
                                <asp:BoundField DataField="categoryName" HeaderText="Category" ItemStyle-Width="100px" />
                            </Columns>
                        </asp:GridView>

背后的代码:

 //Portion to check the storage level for each products stored in tempList
        //Loop thru tempList. key as prod variant ID, tempList.Keys as quantity
        foreach (string key in tempList.Keys)
        {
            //Get total unit of each products
            totalUnit = prodPackBLL.getTotalProductUnit(key);
            valid = true;

            //Check if unitQuantity exceed storage level
            if (((Convert.ToInt32(tempList[key])) * packagesNeeded) > totalUnit)
            {
                //Get the label control in gridview
                foreach (GridViewRow gr in gvFinalised.Rows)
                {
                    if (key == gvFinalised.DataKeys[gr.RowIndex].Value.ToString())
                    {
                        //Change the color of textBox and display the insufficient message
                        valid = false;
                        //Automatically uncheck the checkBox if invalid
                        TextBox tb = (TextBox)gr.FindControl("tbQuantity") as TextBox;
                        tb.CssClass = "alert alert-danger";
                        tb.Attributes["style"] = "height: 3px; width: 50px; margin-bottom: 0px; padding-left: 0px";
                        Label lblCheckAmount = gr.FindControl("lblCheckAmount") as Label;
                        lblCheckAmount.Text = "Insufficient stock!";

                        getSuggested(key);
                    }
                }
            }

如果库存不足,请通过传递ID来调用getSuggested():

 protected void getSuggested(string prodVariantID)
    {
        string categoryName = prodPackBLL.getCategoryByProdVariantID(prodVariantID);

        //Get the list of substitute product with highest storage level  sorted in descending order
        List<ProductPacking> prodSubstitute = new List<ProductPacking>();
        List<string> lstCategory = new List<string>();
        List<string> prodIDList = new List<string>();
        List<DistributionStandardPackingUnitItems> distSPUItem = new List<DistributionStandardPackingUnitItems>();

        //Find list of substitute with highest stock level and replace the product
        prodSubstitute = prodPackBLL.getProductIDWithHighestStock(categoryName);

        for (int count = 0; count < prodSubstitute.Count; count++)
        {
            //To prevent duplication of same product and select those catories which are in current category and counting them and taking them if there are less than 1 occurrences
            if (!prodIDList.Contains(prodSubstitute[count].id) && !(lstCategory.Where(x => x.Equals(categoryName)).Select(x => x).Count() >= 1))
            {
                prodIDList.Add(prodSubstitute[count].id);
                lstCategory.Add(categoryName);
            }
        }

        for (int j = 0; j < prodIDList.Count; j++)
        {
            //Get the detail of the product added into prodList and add it into distSPUItem List
            distSPUItem.Add(packBLL.getSPUItemDetailByID(prodIDList[j]));
        }

        gvSuggested.DataSource = distSPUItemList;
        gvSuggested.DataBind();
    }

获取最高产品库存水平降序的SQL方法:

public List<ProductPacking> getProductIDWithHighestStock(string categoryName)
    {
        List<ProductPacking> prodSubstitute = new List<ProductPacking>();

        using (var connection = new SqlConnection(FoodBankDB.connectionString))
        {
            SqlCommand command = new SqlCommand("SELECT p.id, p.inventoryQuantity FROM dbo.Products p " +
                " INNER JOIN dbo.ProductVariants pv ON p.id = pv.product " +
                " INNER JOIN dbo.ProductCategories pc ON p.productCategory = pc.id " +
                " WHERE pc.categoryName = '" + categoryName + "'  " +
                " ORDER BY Convert(INT, p.inventoryQuantity) DESC", connection);
            connection.Open();
            using (var dr = command.ExecuteReader())
            {
                while (dr.Read())
                {
                    string prodID = dr["id"].ToString();

                    prodSubstitute.Add(new ProductPacking(prodID));
                }
            }
        }

        return prodSubstitute;
    }

获取要在gridview建议中显示的项目详细信息的SQL方法:

public DistributionStandardPackingUnitItems getSPUItemDetailByID(string prodID)
    {
        DistributionStandardPackingUnitItems item = new DistributionStandardPackingUnitItems();

        using (var connection = new SqlConnection(FoodBankDB.connectionString))
        {
            SqlCommand command = new SqlCommand("SELECT p.id, p.name, p.description, pc.categoryName FROM dbo.Products p " +
                " INNER JOIN dbo.ProductCategories pc ON p.productCategory = pc.id " +
                " WHERE p.id = '" + prodID + "'" +
                " ORDER BY pc.categoryName ", connection);
            connection.Open();
            using (var dr = command.ExecuteReader())
            {
                while (dr.Read())
                {
                    string id = dr["id"].ToString();
                    string name = dr["name"].ToString();
                    string description = dr["description"].ToString();
                    string categoryName = dr["categoryName"].ToString();

                    item = new DistributionStandardPackingUnitItems(id, name, description, categoryName, "");
                }
            }
        }

        return item;
    }

但是,当我运行该程序时,它会给出一条错误消息:在所选数据源上找不到名称为“name”的字段或属性。我不知道为什么会这样,当我在调试模式下运行时,它确实返回了我所有的值。只是它不会在网格视图中显示。

提前致谢。

修改

假设我将产品1,2,3添加到列表中,而产品1是库存水平最高的产品。产品2和3不足。因此,当我执行它时,由于产品1已经在列表中,因此系统应该找到第二高的并且替换为产品2.在替换产品2之后,系统应该将产品3替换为第三高。我的prodSubstitute已按降序排序,我只是想知道if循环中的if语句,我应该如何实现这个逻辑?

1 个答案:

答案 0 :(得分:0)

由于prodSubstitute已按inventoryQuantity按降序排序,因此您可以使用prodSubstitute[n-1]

获取第n个最高数量