删除购物车中的行 [英] deletion of rows in cart

查看:88
本文介绍了删除购物车中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有个问题.目前,我正在使用asp.net和C#.net准备一个购物车网站.每当客户端登录时,我都会读取数据表db并分配数据表,并分配给购物车并在网格视图中加载.我的问题是在购物车未更新时.它显示了异常.也就是说,已删除的表格无法将数据分配到变量中.但是它适用于普通购物车,即,当购买产品时,它将添加到购物车和数据库中.
有人可以帮我吗?
我的代码是:

Hi,

I have a problem. Presently I''m preparing a shopping cart website using asp.net and C#.net. Whenever client logs in I read the data form db and assign data table and assign to cart and load in grid view. My problem is at the time of edition the cart is not updated. It shows the exception. i.e deleted table can''t assign the data into variables. But it will work for normal cart, i.e when purchase a product it will add to cart and db.
Can anybody help me?
My code is:

objDT = (DataTable)HttpContext.Current.Session["Cart"];
            decimal subTotalBefDiscount = default(decimal);
            for (int i = 0; i < objDT.Rows.Count; i++)
            {
                objDR = objDT.Rows[i];
                string sQuantity = ((TextBox)gvCart.Rows[i].FindControl("txtQuantity")).Text;
                objDR["Quantity"] = sQuantity;
                objDR["Price"] = objDR["Price"];
                int qty = Convert.ToInt32(objDR["Quantity"].ToString());
                decimal price = Convert.ToDecimal(objDR["Price"]);
                objDR["Total"] = qty * price;
            }
            int rowCount = objDT.Rows.Count;
            //objDT.Reset();
            for (int i = rowCount - 1; i >= 0; i--)
            {
                CheckBox cbDelete = (CheckBox)gvCart.Rows[i].Cells[0].FindControl("chkDelete");
                if (cbDelete.Checked)
                {
                    objDT.Rows[i].Delete();
                }
            }
            for (int i = 0; i < objDT.Rows.Count; i++)
            {
                objDR = objDT.Rows[i];
                int qty = Convert.ToInt32(objDR["Quantity"].ToString());
                decimal price = Convert.ToDecimal(objDR["Price"]);
                decimal tot = qty * price;
                subTotalBefDiscount = subTotalBefDiscount + tot;
            }
            gvCart.DataSource = objDT;
            gvCart.DataBind();
            lblsubTotalBefDiscount.Text = subTotalBefDiscount.ToString();
            decimal QuantityDiscounts = Caluculate_QunatityDsicounts();
            lblQuantityDiscounts.Text = QuantityDiscounts.ToString();
            lblsubTotalAftDiscount.Text = (subTotalBefDiscount - QuantityDiscounts).ToString();
            if (gvCart.Rows.Count > 0)
            {
                plcShoppingCart.Visible = true;
                plcEmptyCart.Visible = false;
            }
            else
            {
                plcShoppingCart.Visible = false;
                plcEmptyCart.Visible = true;
            }
            StoreBillingInfoInSession();
        }
        catch (Exception Ex)
        {
            Response.Redirect("Error.aspx?M=" + Ex.Message);
        }


谢谢.

推荐答案

我发现当没有行要显示时,您隐藏了gridview,因此您不需要执行所有的for循环和如果购物车是空的,请进行装订.因此,如果objDT.Rows.Count为零,则可以立即转到隐藏gridview和
的部分 仅在购物车中有 I 时才做其他事情.

修改:
天哪,我在MSDN上找到了解决您问题的方法.在该行的Delete调用之后,您还必须调用AcceptChanges().只有这样,该行才会真正从表中删除.

I spotted that you are hiding the gridview when there are no rows to display so you don''t need to do all the for loops and the binding in case of an empty cart. So if objDT.Rows.Count is zero you can go immeadiately to the part where you are hiding the gridview and
do the other stuff only when there is something I in your cart.

Modification:
OMG, I found the solution to your problem on MSDN. After the Delete call on the row you also have to call AcceptChanges(). Only then will the row really be removed from the table.

// Going through the grid rows to see which entries shall be deletee
for (int i = rowCount - 1; i >= 0; i--)
{
    CheckBox cbDelete = (CheckBox)gvCart.Rows[i].Cells[0].FindControl("chkDelete");
    if (cbDelete.Checked)
    {
        objDT.Rows[i].Delete(); // So far so good the row can be removed from the table
        objDT.Rows[i].AcceptChanges(); // Now the row will now really be removed
    }
}


上面的代码应该可以解决您的问题.

结束修改


问候,
曼弗雷德(Manfred)


The above code should solve your issue.

End modification


Regards,
Manfred


这篇关于删除购物车中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆