在gridview中添加具有相同值的新行后更新一个单元格

时间:2012-06-29 14:46:31

标签: asp.net gridview datatable

我想制作购物车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中添加新记录),就像我在图像上标记一样

enter image description here

2 个答案:

答案 0 :(得分:0)

经过长时间的测试和googleing后我终于完成了,这里是我使用的代码

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;
}

答案 1 :(得分: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