Пример #1
0
        private void dgvOrderBook_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            string columnName = this.dgvOrderBook.Columns[e.ColumnIndex].Name;

            int temp = -1;

            if (columnName.Equals("Quantity"))
            {
                if (string.IsNullOrEmpty(e.FormattedValue.ToString()))
                {
                    this.dgvOrderBook.Rows[e.RowIndex].ErrorText = "Quantity could not be empty.";
                    e.Cancel = true;
                }
                else
                {
                    bool check = false;
                    try
                    {
                        temp = int.Parse(e.FormattedValue.ToString());
                    }
                    catch (Exception)
                    {
                    }
                    if (temp > 0)
                    {
                        int current = dgvOrderBook.CurrentCell.RowIndex;

                        int       ID          = int.Parse(dgvOrderBook.Rows[current].Cells["ID"].Value.ToString());
                        DataRow[] rows        = datatable.Select("ID =" + ID);
                        int       maxQuantity = int.Parse(rows[0]["Quantity"].ToString());
                        if (temp <= maxQuantity)
                        {
                            BookOrderObj re = OrderList.SingleOrDefault(o => o.ID == ID);
                            if (re != null)
                            {
                                check       = true;
                                re.Quantity = temp;

                                double TotalPrice = 0;
                                foreach (var item in OrderList)
                                {
                                    TotalPrice += (item.Price * item.Quantity);
                                }

                                lblTotal.Text = "Total: " + TotalPrice + " $";
                            }
                        }
                    }
                    if (check == false)
                    {
                        this.dgvOrderBook.Rows[e.RowIndex].ErrorText = "Quantity only number and small than available book quantity.";
                        e.Cancel = true;
                    }
                }
            }
        }
Пример #2
0
        private void dgvBook_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                int          ID            = int.Parse(dgvBook.Rows[e.RowIndex].Cells["ID"].Value.ToString());
                string       ISBN          = dgvBook.Rows[e.RowIndex].Cells["ISBN"].Value.ToString();
                string       Name          = dgvBook.Rows[e.RowIndex].Cells["Name"].Value.ToString();
                int          MaxQuantity   = int.Parse(dgvBook.Rows[e.RowIndex].Cells["Quantity"].Value.ToString());
                double       Price         = float.Parse(dgvBook.Rows[e.RowIndex].Cells["Price"].Value.ToString());
                int          OrderQuantity = 1;
                BookOrderObj re            = OrderList.SingleOrDefault(o => o.ID == ID);
                if (re != null)
                {
                    if (MaxQuantity == re.Quantity)
                    {
                        MessageBox.Show("You can add more quantity.");
                        return;
                    }
                    OrderQuantity = re.Quantity + 1;
                    re.Quantity   = OrderQuantity;
                }
                else
                {
                    OrderList.Add(new BookOrderObj
                    {
                        ID       = ID,
                        ISBN     = ISBN,
                        Name     = Name,
                        Quantity = 1,
                        Price    = Price
                    });
                }


                var     bindingList = new BindingList <BookOrderObj>(OrderList);
                dynamic source      = new BindingSource(bindingList, null);

                double TotalPrice = 0;
                foreach (var item in OrderList)
                {
                    TotalPrice += (item.Price * item.Quantity);
                }

                lblTotal.Text           = "Total: " + TotalPrice + " $";
                dgvOrderBook.DataSource = source;
            }
            else
            {
                return;
            }
        }
Пример #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (dgvOrderBook.Rows.Count <= 0)
            {
                MessageBox.Show("Please choose one item.");
                return;
            }
            int current = dgvOrderBook.CurrentCell.RowIndex;

            if (current >= 0)
            {
                int ID = int.Parse(dgvOrderBook.Rows[current].Cells["ID"].Value.ToString());

                DialogResult r = MessageBox.Show("Do you want delete this items?", "Confirm Delete", MessageBoxButtons.YesNo);

                if (r == DialogResult.Yes)
                {
                    BookOrderObj re = OrderList.SingleOrDefault(o => o.ID == ID);
                    if (re != null)
                    {
                        OrderList.Remove(re);
                    }
                    else
                    {
                        return;
                    }
                }


                var     bindingList = new BindingList <BookOrderObj>(OrderList);
                dynamic source      = new BindingSource(bindingList, null);
                dgvOrderBook.DataSource = source;

                double TotalPrice = 0;
                foreach (var item in OrderList)
                {
                    TotalPrice += (item.Price * item.Quantity);
                }

                lblTotal.Text           = "Total: " + TotalPrice + " $";
                dgvOrderBook.DataSource = source;
            }
            else
            {
                return;
            }
        }