比较数据表中的值

时间:2012-05-01 21:44:19

标签: c# asp.net datatable

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

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;

}

2 个答案:

答案 0 :(得分:0)

你运行这段代码:

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

每次条件为假时:title == checktitle

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

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或向其添加行的确切语法。

答案 1 :(得分:0)

也许你可以使用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();
            }
     }

 }

你的CartRow

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;

}