예제 #1
0
        private void save_Click(object sender, EventArgs e)
        {
            using (DocContext db = new DocContext())
            {
                Doc newDoc = new Doc();
                newDoc.DocCreateTime = DateTime.Now;
                newDoc.DocType       = comboBox1.SelectedItem.ToString();
                TablePart tablePart = new TablePart();
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    string nameCellValue = dataGridView1.Rows[i].Cells[1].Value.ToString();
                    if (db.Goods.FirstOrDefault(Good => Good.name == nameCellValue) != null)
                    {
                        TablePartString tablePartString = new TablePartString();
                        tablePartString.serial   = i;
                        tablePartString.Good     = db.Goods.FirstOrDefault(Good => Good.name == nameCellValue);
                        tablePartString.quantity = int.Parse(dataGridView1.Rows[i].Cells[2].Value.ToString());
                        tablePartString.balance  = int.Parse(dataGridView1.Rows[i].Cells[3].Value.ToString());
                        tablePart.AddString(tablePartString);
                    }
                    else
                    {
                        MessageBox.Show("Документ НЕ сохранен");
                        return;
                    }
                }
                newDoc.DocTablePart = tablePart;


                db.Docs.Add(newDoc);
                foreach (TablePartString stringWhithGoodForAddBalance in tablePart)
                {
                    string goodNameForAddBalance = stringWhithGoodForAddBalance.Good.name.ToString();
                    Good   goodForAddBalance     = db.Goods.FirstOrDefault(Good => Good.name == goodNameForAddBalance);
                    goodForAddBalance.balance += int.Parse(stringWhithGoodForAddBalance.balance.ToString());
                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Не удалось сохранить изменения в " + stringWhithGoodForAddBalance.serial.ToString() + " строке документа!");
                        throw;
                    }
                }
                //string goodName = dataGridView1.Rows[0].Cells[1].Value.ToString();
                //Good goodItem = db.Goods.FirstOrDefault(Good => Good.name == goodName);
                //goodItem.balance += int.Parse(dataGridView1.Rows[0].Cells[2].Value.ToString());
                //db.SaveChanges();


                MessageBox.Show("Документ сохранен");
            }
        }
예제 #2
0
 public int Count(TablePart tablePart)
 {
     return(Strings.Count);
 }