Example #1
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if ((Convert.ToInt32(txtNumberCount.Text) <= 0) || (txtPrice.Text.Trim() == ""))
                {
                    MessageBox.Show("Please add number and price", "Add all...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                //if (_isPurchase == false)
                //{
                if (Convert.ToInt32(txtNumberCount.Text) > Convert.ToInt32(lblNoInStock.Text))
                {
                    MessageBox.Show("Out of Stock", "Wrong Add...", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                int totalSale = 0;

                if (grdViewStock.Rows.Count > 0)
                {
                    int i = 0;

                    totalSale = Convert.ToInt32(txtNumberCount.Text);
                    foreach (DataGridViewRow row in grdViewStock.Rows)
                    {
                        if (Convert.ToInt32(row.Cells[9].Value) == Convert.ToInt32(cmbInventory.SelectedValue))
                        {
                            totalSale = totalSale + Convert.ToInt32(row.Cells[4].Value);
                            i++;
                        }
                    }
                    if (i > 0)
                    {
                        if (totalSale > Convert.ToInt32(lblNoInStock.Text))
                        {
                            MessageBox.Show("Out of Stock", "Wrong Add...", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                }
                //}

                Stock stk = new Stock()
                {
                    StockId           = 0,
                    InventoryTypeId   = Convert.ToInt32(cmbInventoryType.SelectedValue),
                    InventoryId       = Convert.ToInt32(cmbInventory.SelectedValue),
                    Price             = Convert.ToDecimal(txtPrice.Text),
                    Description       = txtDescription.Text,
                    IsPurchase        = _isPurchase,
                    ProcessDate       = dtpProcessDate.Value,
                    InventoryTypeName = (cmbInventoryType.SelectedItem as LookupDO).Text,
                    InventoryName     = (cmbInventory.SelectedItem as LookupDO).Text,
                    InventoryNumber   = txtInventoryNumber.Text,
                    NumberCount       = Convert.ToInt32(txtNumberCount.Text)
                };

                if (btnAdd.Text == "Update")
                {
                    if (lblStkId.Text != "0")
                    {
                        stk.StockId = Convert.ToInt32(lblStkId.Text);
                        _repository.SaveStock(stk);
                        MessageBox.Show("Stock Updated Successfully", "Updated...", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        lblNoInStock.Text = Convert.ToString(_repository.GetNumberInStock(Convert.ToInt32(cmbInventory.SelectedValue)));
                    }
                    else
                    {
                        var dataSource = grdViewStock.DataSource;

                        foreach (DataGridViewRow row in grdViewStock.Rows)
                        {
                            var thsStk = row.DataBoundItem as Stock;
                            if (tempStk == thsStk)
                            {
                                (row.DataBoundItem as Stock).InventoryTypeId   = stk.InventoryTypeId;
                                (row.DataBoundItem as Stock).InventoryId       = stk.InventoryId;
                                (row.DataBoundItem as Stock).Price             = stk.Price;
                                (row.DataBoundItem as Stock).Description       = stk.Description;
                                (row.DataBoundItem as Stock).IsPurchase        = stk.IsPurchase;
                                (row.DataBoundItem as Stock).ProcessDate       = stk.ProcessDate;
                                (row.DataBoundItem as Stock).InventoryTypeName = stk.InventoryTypeName;
                                (row.DataBoundItem as Stock).InventoryName     = stk.InventoryName;
                                (row.DataBoundItem as Stock).InventoryNumber   = stk.InventoryNumber;
                                (row.DataBoundItem as Stock).NumberCount       = stk.NumberCount;
                                break;
                            }
                        }
                        grdViewStock.Refresh();
                    }
                    btnAdd.Text = "Add";
                }
                else
                {
                    var dataSource = grdViewStock.DataSource;
                    (dataSource as BindingList <Stock>).Add(stk);
                    grdViewStock.DataSource = dataSource;
                    grdViewStock.Refresh();
                    pnlSaveAll.Visible = true;
                }

                ClearAll();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }