2012-05-01 10 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

を追加するために使用していたクラスです:私はchecktitle

==タイトル:

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を初期化したり、行を追加したりするための正確な構文は覚えていません。

+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

関連する問題