Ejemplo n.º 1
0
        private void dataGridViewGroups_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["ColumnEditGroupButton"].Index && e.RowIndex >= 0)
            {
                using (Model.KeynerContext db = new Model.KeynerContext())
                {
                    Model.Group  group = db.GroupSet.FirstOrDefault(g => g.Id == id);
                    FormNewGroup form  = new FormNewGroup(ref group);
                    form.ShowDialog();
                    if (form.DialogResult == DialogResult.Yes)
                    {
                        this.FillDataGridGroups();
                    }
                }
            }

            if (e.ColumnIndex == dataGrid.Columns["ColumnDeleteGroupButton"].Index && e.RowIndex >= 0)
            {
                if (MessageBox.Show("Do you want delete this group?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    using (Model.KeynerContext db = new Model.KeynerContext())
                    {
                        db.Entry(db.GroupSet.FirstOrDefault(g => g.Id == id)).State = System.Data.Entity.EntityState.Deleted;
                        db.SaveChanges();
                        dataGrid.Rows.RemoveAt(e.RowIndex);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void buttonSaveGroup_Click(object sender, EventArgs e)
        {
            if (_group == null)
            {
                _group = new Model.Group();
            }

            _group.Name = textBoxGroupName.Text;

            List <ValidationResult> validationResults = new List <ValidationResult>();
            ValidationContext       validationContext = new ValidationContext(_group);

            if (!Validator.TryValidateObject(_group, 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(_group).State = System.Data.Entity.EntityState.Modified;
                }
                else
                {
                    db.GroupSet.Add(_group);
                }
                db.SaveChanges();
                this.DialogResult = DialogResult.Yes;
                this.Close();
            }
        }
Ejemplo n.º 3
0
 public FormNewGroup(ref Model.Group group) : this()
 {
     this._group           = group;
     textBoxGroupName.Text = _group.Name;
     _modified             = true;
 }