如何将项目和数量放入下拉列表中

时间:2015-09-28 17:14:50

标签: c# asp.net drop-down-menu

我想在下拉列表中显示库存表中剩余的数量。我无法显示库存的剩余数量

<div class="form-group">
            <label class="control-label col-lg-4">Material</label>
            <div class="col-lg-8">
                <asp:DropDownList ID="ddlMaterial" runat="server" class="form-control" AutoPostBack="true" onselectedindexchanged="ddlMaterial_SelectedIndexChanged" />
            </div>
        </div>

这是获得库存的cs

void GetMaterials()
{
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText = "SELECT Products.ProductID, ProductName, Inventory.Quantity FROM Products INNER JOIN Inventory ON Inventory.ProductID = Products.ProductID";
    SqlDataReader dr = cmd.ExecuteReader();
    ddlMaterials.DataSource = dr;
    ddlMaterials.DataTextField = "ProductName" + "Quantity";
    ddlMaterials.DataValueField = "ProductID";
    ddlMaterials.DataBind();
    con.Close();
}

我想看起来像= grape(6),apple(4)和mango(0)

1 个答案:

答案 0 :(得分:0)

一种方法是更新SQL,例如

SELECT Products.ProductID, ProductName + '(' + convert(nvarchar,Inventory.Quantity) + ')' as ProdInventory FROM Products INNER JOIN Inventory ON Inventory.ProductID = Products.ProductID

然后

ddlMaterials.DataTextField = "ProdInventory";
ddlMaterials.DataValueField = "ProductID";

P.S。未经测试的代码。