比较数据表中的值

本文关键字:数据表 比较 | 更新日期: 2023-09-27 18:32:47

我正在尝试处理数据表以将新值与现有数据表进行比较,如果可用,数量将相加,但如果没有,它将在数据表中添加一行。

if (HttpContext.Current.Session["Cart"] != null)
{
    DataTable shoppingcart = (DataTable)HttpContext.Current.Session["Cart"];
    for (int i = 0; i < shoppingcart.Rows.Count; i++)
    {
        String checktitle = shoppingcart.Rows[i]["Title"].ToString();
            if (title == checktitle)
            {
                //do something
            }
            else
            {
                ShoppingCart.DataSource = cart.cartrow(shoppingcart);
                ShoppingCart.DataBind();
            }
        }
    }
    else
    {
        HttpContext.Current.Session["Cart"] = cart.shoppingCart();
        ShoppingCart.DataSource = cart.shoppingCart();
        ShoppingCart.DataBind();
    }
}  

但不知何故。我无法添加它的数量。它每次都在创建一个新行。谢谢你的建议。这是我用来添加行或添加表的类

String title { get; set; }
decimal price { get; set; }
int quantity { get; set; }
DataTable CartTable;
DataRow tableRow;
public cart(String _title, decimal _price)
{
    title = _title;
    price = _price;
}
public DataTable shoppingCart()
{
    CartTable = new DataTable("cart");
    CartTable.Columns.Add("ID", typeof(Int32));
    CartTable.Columns["ID"].AutoIncrement = true;
    CartTable.Columns["ID"].AutoIncrementSeed = 1;
    CartTable.Columns.Add("Title");
    CartTable.Columns.Add("Price");
    CartTable.Columns.Add("quantity");

    tableRow = CartTable.NewRow();
    tableRow["Title"] = title;
    tableRow["Price"] = price;
    tableRow["quantity"] = quantity;
    CartTable.Rows.Add(tableRow);
    return CartTable;
}
public DataTable cartrow(DataTable _cart)
{
    tableRow = _cart.NewRow();
    tableRow["Title"] = title;
    tableRow["Price"] = price;
    tableRow["quantity"] = quantity;
    _cart.Rows.Add(tableRow);
    return _cart;
}

比较数据表中的值

运行以下代码:

ShoppingCart.DataSource = cart.cartrow(shoppingcart);
ShoppingCart.DataBind();

每次您的条件为假时:标题 == 检查标题

我不确定这是错误,但你应该构建一些不同的东西,比如这样:

if (HttpContext.Current.Session["Cart"] != null)
{
    DataTable shoppingcart = (DataTable)HttpContext.Current.Session["Cart"];
    //put here each row you need to bind
    DataTable toBind = new DataTable();  //do more to init the columns, rows, etc
    for (int i = 0; i < shoppingcart.Rows.Count; i++)
    {
        String checktitle = shoppingcart.Rows[i]["Title"].ToString();
            if (title == checktitle)
            {
                //do something
            }
            else
            {
                toBind.add(cart.cartrow(shoppingcart));  //the sintax here is incorect, I code directly here
                //ShoppingCart.DataSource = cart.cartrow(shoppingcart);
                //ShoppingCart.DataBind();
            }
        }
        //bind outside the for cicle
        ShoppingCart.DataSource = toBind ;
        ShoppingCart.DataBind();
    }
    else
    {
        HttpContext.Current.Session["Cart"] = cart.shoppingCart();
        ShoppingCart.DataSource = cart.shoppingCart();
        ShoppingCart.DataBind();
    }
}  

我希望这能给你一个纠正代码的方向。我直接将其编码到浏览器中,我不记得初始化DataTable或向其添加行的确切语法。

也许你可以使用 foreach:

if (HttpContext.Current.Session["Cart"] != null)
{
    DataTable shoppingcart = (DataTable)HttpContext.Current.Session["Cart"];
    foreach (DataRow row in shoppingcart.Rows)
    {
            String checktitle = row["Title"].ToString();
            int price = row["price"].ToString();
            int quantity = row["quantity"].ToString();
            if (title == checktitle)
            {
                //do something
            }
            else
            {
                Session["Cart"] = cart.cartrow(shoppingcart,checktitle,price,quantity);
                ShoppingCart.DataSource = Session["Cart"]  as DataTable();
                ShoppingCart.DataBind();
            }
     }
 }

您的购物车行

public DataTable cartrow(DataTable _cart,string title,int price,int quantity)
{
    tableRow = _cart.NewRow();
    tableRow["Title"] = title;
    tableRow["Price"] = price;
    tableRow["quantity"] = quantity;
    _cart.Rows.Add(tableRow);
    return _cart;
}