public FormNewMonster(ref Model.Monster monster) : this() { this._monster = monster; textBoxMonsterName.Text = _monster.Name; _modified = true; this.FillDataGridLevels(); }
private void buttonSaveMonster_Click(object sender, EventArgs e) { if (_monster == null) { _monster = new Model.Monster(); } _monster.Name = textBoxMonsterName.Text; List <ValidationResult> validationResults = new List <ValidationResult>(); ValidationContext validationContext = new ValidationContext(_monster); if (!Validator.TryValidateObject(_monster, validationContext, validationResults, true)) { string errorMesseges = ""; foreach (ValidationResult item in validationResults) { errorMesseges += item.ErrorMessage + "\n"; } MessageBox.Show(errorMesseges, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } using (Model.KeynerContext db = new Model.KeynerContext()) { if (_modified) { db.Entry(_monster).State = System.Data.Entity.EntityState.Modified; } else { db.MonsterSet.Add(_monster); } db.SaveChanges(); } if (_modified == false) { buttonNewLevel.Visible = true; _modified = true; } else { this.Close(); } }
private void dataGridViewMonsters_CellContentClick(object sender, DataGridViewCellEventArgs e) { DataGridView dataGrid = (DataGridView)sender; DataGridViewButtonColumn button = dataGrid.Columns[e.ColumnIndex] as DataGridViewButtonColumn; int id = ((int)dataGrid.Rows[e.RowIndex].Cells[0].Value); if (e.ColumnIndex == dataGrid.Columns["ColumnEditMonsterButton"].Index && e.RowIndex >= 0) { using (Model.KeynerContext db = new Model.KeynerContext()) { Model.Monster monster = db.MonsterSet.FirstOrDefault(m => m.Id == id); FormNewMonster form = new FormNewMonster(ref monster); form.ShowDialog(); if (form.DialogResult == DialogResult.Yes) { this.FillDataGridMonsters(); } } } if (e.ColumnIndex == dataGrid.Columns["ColumnDeleteMonsterButton"].Index && e.RowIndex >= 0) { if (MessageBox.Show("Do you want delete this monster?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { using (Model.KeynerContext db = new Model.KeynerContext()) { db.Entry(db.MonsterSet.FirstOrDefault(m => m.Id == id)).State = System.Data.Entity.EntityState.Deleted; foreach (Model.MonsterLevel item in db.MonsterLevelSet.Where(l => l.Id_Monster == id).ToList()) { db.Entry(item).State = System.Data.Entity.EntityState.Deleted; } db.SaveChanges(); dataGrid.Rows.RemoveAt(e.RowIndex); } } } }