2012-05-01 86 views
0

我想在數據表上工作,以便將新值與現有數據表進行比較(如果可用),數量將相加,但如果沒有,則會在數據表中添加一行。比較數據表中的值

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; 

} 
+0

您的代碼不告訴你在問什麼。你在哪裏比較價值,添加一個數量,並創建一個新的行? –

+0

對不起,我是編程新手。我創建了一個類來運行我添加的行和表 – eugene

回答

0

你運行該代碼的類:每個條件爲假時

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

:標題== 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或向它添加一行。

+0

謝謝,但它沒有奏效。 – eugene

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; 

} 
+0

對不起,我嘗試使用foreach,但有一個錯誤 – eugene

+0

什麼是錯誤? – BizApps

+0

收藏已修改;枚舉操作可能不會執行。 – eugene