0

我想制作购物车Datatable,我在这里写了以下代码来制作产品列表

public  DataTable ShoppingCartlist(string proname , string country, 
    string area ,string address,int quantity,decimal price,DateTime date)
{
    DataTable dt = new DataTable();

    if (HttpContext.Current.Session["ShoppingList"]==null)
    {  
        dt.Columns.Add("ProName");
        dt.Columns.Add("odate");
        dt.Columns.Add("ocountry");
        dt.Columns.Add("oarea");
        dt.Columns.Add("oaddress");
        dt.Columns.Add("quantity");
        dt.Columns.Add("price");
        dt.Columns.Add("SubTotal");

        dt.Constraints.Add("Proid_PK", dt.Columns[0], true);
    }
    else
    {
        dt = (DataTable)HttpContext.Current.Session["ShoppingList"];

        DataRow dr = dt.NewRow();
        dr[0] = proname;
        dr[1] = date;
        dr[2] = country;
        dr[3] = area;
        dr[4] = address;
        dr[5] = quantity;
        dr[6] = price;
        dr[7] = quantity * price;

        dt.Rows.Add(dr);      
    }
    return dt;
}

它可以很好地绑定数据,没有问题,但是我在gridview的新记录中插入具有相同值的同一行,我不希望它像我只想更新一个单元格(我的意思是当我添加新记录时具有相同的值,我希望Quantity增加值而不在gridview中添加新记录)就像我在图像上标记的那样

在此处输入图像描述

4

2 回答 2

0

经过长时间的测试和谷歌搜索,我终于完成了,这里是我使用的代码

public  DataTable ShoppingCartlist(string proname , string country, 
    string area ,string address,int quantity,decimal price,DateTime date)
{
      DataTable dt = new DataTable();
    if (HttpContext.Current.Session["ShoppingList"]==null)
    {  
    dt.Columns.Add("ProName");
    dt.Columns.Add("odate");
    dt.Columns.Add("ocountry");
    dt.Columns.Add("oarea");
    dt.Columns.Add("oaddress");
    dt.Columns.Add("quantity");
    dt.Columns.Add("price");
    dt.Columns.Add("SubTotal");

   DataRow dr = dt.NewRow();
    dr[0] = proname;
    dr[1] = date;
    dr[2] = country;
    dr[3] = area;
    dr[4] = address;
    dr[5] = quantity;
    dr[6] = price;
    dr[7] = quantity * price;
    dt.Rows.Add(dr); 
    }
    else
    {
      dt = (DataTable)HttpContext.Current.Session["ShoppingList"];

    DataView dv = dt.DefaultView;
    dv.Sort = "ProName";
    int found;
    string Proname;
      DataRow dr;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dr = dt.Rows[i];
          Proname = dr[0].ToString();
       found = dv.Find(Proname);


        if (found != -1)
        {
            foreach (DataRow pro in dt.Rows)
            {
                for (int x = 0; x < dt.Rows.Count; x++)
            {
                if (dt.Rows[x]["ProName"].ToString()==Proname)
                {
                    dt.Rows[x]["quantity"] = Convert.ToInt16(dt.Rows[x]["quantity"]) + quantity;
                }
            }
            } 
        }   
        }          
        }
   return dt;
}
于 2012-06-30T06:09:02.620 回答
0

您可以使用 LINQ:

DataTable dt = new DataTable("products"); 
dt.Columns.Add("ProName", typeof(string)); 
dt.Columns.Add("odate", typeof(DateTime)); 
dt.Columns.Add("ocountry", typeof(string)); 
dt.Columns.Add("oarea", typeof(string)); 
dt.Columns.Add("oaddress", typeof(string)); 
dt.Columns.Add("quantity", typeof(int)); 
dt.Columns.Add("price", typeof(decimal)); 
dt.Columns.Add("SubTotal", typeof(decimal)); 

DataRow dr1 = dt.NewRow(); 

dr1[0] = "test"; 
dr1[1] = new DateTime(2005, 5, 15); 
dr1[2] = "US"; 
dr1[3] = "test area"; 
dr1[4] = "555"; 
dr1[5] = 1; 
dr1[6] = 5.25; 
dr1[7] = 5.25 * 1;

dt.Rows.Add(dr1);

DataRow dr2 = dt.NewRow();

dr2[0] = "test";
dr2[1] = new DateTime(2005, 5, 15);
dr2[2] = "US";
dr2[3] = "test area";
dr2[4] = "555";
dr2[5] = 3;
dr2[6] = 5.25;
dr2[7] = 5.25 * 3; 

dt.Rows.Add(dr2);

var shoppingCart = from r in dt.AsEnumerable()
                   group r by new
                   {
                       ProName = r.Field<string>("ProName"),
                       odate = r.Field<DateTime>("odate"),
                       ocountry = r.Field<string>("ocountry"),
                       oarea = r.Field<string>("oarea"),
                       oaddress = r.Field<string>("oaddress"),
                       price = r.Field<decimal>("price")
                   } into g
                   select new
                   {
                       g.Key.ProName,
                       g.Key.odate,
                       g.Key.ocountry,
                       g.Key.oarea,
                       g.Key.oaddress,
                       g.Key.price,
                       quantity = g.Sum(s1 => s1.Field<int>("quantity")),
                       SubTotal = g.Sum(s2 => s2.Field<decimal>("SubTotal"))
                   };

foreach (var p in shoppingCart)
{
    Console.WriteLine("Product: {0}, Quantity: {1}, Subtotal: {2}", p.ProName, p.quantity, p.SubTotal);
}

结果:

产品:测试,数量:4,小计:21.00

于 2012-06-30T11:06:05.850 回答