//кнопка добавления объекта Inspection в таблицу InspectionTableForm private void btnAdd_Click(object sender, EventArgs e) { InspectionForm frmIns = new InspectionForm(); DialogResult result = frmIns.ShowDialog(this); if (result == DialogResult.Cancel) { return; } Inspection inspection = new Inspection(); List <Car> cars = db.Cars.ToList(); for (int i = 0; i < cars.Count; i++) { if (cars[i].Id == MainForm.Global_SelectRowMainForm) { addCar = cars[i]; break; } } inspection.DateInspection = frmIns.dateTimePickerDateInspection.Text; inspection.NumberInspection = Convert.ToInt32(frmIns.textBoxNumberInspection.Text); inspection.Car = (Car)addCar; db.Inspections.Add(inspection); db.SaveChanges(); //рефреш, запускается только после добавления второго экземпляра dataGridViewInspectionTableForm.DataSource = db.Inspections.Where(p => p.CarId == MainForm.Global_SelectRowMainForm).ToList(); // обновляем грид MessageBox.Show("Объект обновлен"); }
//кнопка редактирования выбранного объекта Inspection в таблице InspectionTableForm private void btnEdit_Click(object sender, EventArgs e) { if (dataGridViewInspectionTableForm.SelectedRows.Count > 0) { int index = dataGridViewInspectionTableForm.SelectedRows[0].Index; int id = 0; bool converted = Int32.TryParse(dataGridViewInspectionTableForm[0, index].Value.ToString(), out id); if (converted == false) { return; } Inspection inspection = db.Inspections.Find(id); InspectionForm frmIns = new InspectionForm(); frmIns.dateTimePickerDateInspection.Text = inspection.DateInspection; frmIns.textBoxNumberInspection.Text = Convert.ToString(inspection.NumberInspection); List <Car> cars = db.Cars.ToList(); if (inspection.Car != null) { for (int i = 0; i < cars.Count; i++) { if (cars[i].Id == MainForm.Global_SelectRowMainForm) { addCar = cars[i]; break; } } } addCar.Id = inspection.Car.Id; DialogResult result = frmIns.ShowDialog(this); if (result == DialogResult.Cancel) { return; } inspection.DateInspection = frmIns.dateTimePickerDateInspection.Text; inspection.NumberInspection = Convert.ToInt32(frmIns.textBoxNumberInspection.Text); inspection.Car = (Car)addCar; db.Entry(inspection).State = EntityState.Modified; db.SaveChanges(); dataGridViewInspectionTableForm.DataSource = db.Inspections.Where(p => p.CarId == MainForm.Global_SelectRowMainForm).ToList(); // обновляем грид MessageBox.Show("Объект обновлен"); } }