Exemple #1
0
        private void gv_Books_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex > -1 && e.RowIndex > -1 && gv_Books.Rows[e.RowIndex].Cells[this.gv_Books_clm_CartQuantity.Name].Value != null)
            {
                var curBookRow = gv_Books.Rows[e.RowIndex];

                var updated = false;
                foreach (DataGridViewRow cartRow in this.gv_Cart.Rows)
                {
                    if (cartRow.Cells[gv_Cart_clm_Name.Name].Value.ToString() == curBookRow.Cells[gv_Books_clm_Name.Name].Value.ToString())
                    {
                        var quantity        = Convert.ToInt64(curBookRow.Cells[gv_Books_clm_Quantity.Name].Value.ToString());
                        var cartCurQuantity = curBookRow.Cells[gv_Books_clm_CartQuantity.Name].Value.ToString();
                        var cartQuantity    = cartRow.Cells[gv_Cart_clm_Quantity.Name].Value.ToString();
                        var validateResult  = _bookValidator.ValidateAddingCartQuantity(quantity, cartQuantity, cartCurQuantity);
                        if (validateResult.State)
                        {
                            cartRow.Cells[this.gv_Cart_clm_Quantity.Name].Value =
                                Convert.ToInt64(cartRow.Cells[this.gv_Cart_clm_Quantity.Name].Value) + Convert.ToInt64(curBookRow.Cells[this.gv_Books_clm_CartQuantity.Name].Value);
                            cartRow.Cells[this.gv_Cart_clm_Cost.Name].Value
                                = Convert.ToDecimal(curBookRow.Cells[this.gv_Books_clm_Cost.Name].Value) * (Convert.ToInt64(cartRow.Cells[this.gv_Cart_clm_Quantity.Name].Value) + Convert.ToInt64(curBookRow.Cells[this.gv_Books_clm_CartQuantity.Name].Value));
                        }
                        else
                        {
                            curBookRow.Cells[gv_Books_clm_CartQuantity.Name].Value     = "0";
                            curBookRow.Cells[gv_Books_clm_CartQuantity.Name].ErrorText = validateResult.Result;
                        }
                        updated = true;
                        break;
                    }
                }
                if (!updated)
                {
                    var newRowIndex = gv_Cart.Rows.Add();
                    var newRow      = gv_Cart.Rows[newRowIndex];
                    newRow.Cells[this.gv_Cart_clm_BookCost.Name].Value     = curBookRow.Cells[this.gv_Books_clm_Cost.Name].Value;
                    newRow.Cells[this.gv_Cart_clm_BookQuantity.Name].Value = curBookRow.Cells[this.gv_Books_clm_Quantity.Name].Value;
                    newRow.Cells[this.gv_Cart_clm_Name.Name].Value         = curBookRow.Cells[this.gv_Books_clm_Name.Name].Value;
                    newRow.Cells[this.gv_Cart_clm_ID.Name].Value           = curBookRow.Cells[this.gv_Books_clm_ID.Name].Value;
                    newRow.Cells[this.gv_Cart_clm_Quantity.Name].Value     = curBookRow.Cells[this.gv_Books_clm_CartQuantity.Name].Value;
                    newRow.Cells[this.gv_Cart_clm_Cost.Name].Value         = Convert.ToDecimal(curBookRow.Cells[this.gv_Books_clm_Cost.Name].Value) * Convert.ToInt64(curBookRow.Cells[this.gv_Books_clm_CartQuantity.Name].Value);
                }
                UpdateCartInfo();
            }
        }