private void olvSalarie_Click(object sender, EventArgs e) { try { if (gridView1.SelectedRowsCount != 1) { return; } F_Departement row = (F_Departement)(gridView1.GetRow(gridView1.FocusedRowHandle)); txtCode.Text = row.DP_Code; txtIntitule.Text = row.DP_intitule; txtCode.Enabled = false; btSupprimer.Enabled = true; _mode = Mode.Modifier; this.ActiveControl = txtIntitule; txtIntitule.Focus(); } catch (Exception ex) { Program.ErrorMessage(ex.Message); } }
private void btEnregistrer_Click(object sender, EventArgs e) { try { if (txtCode.Text == "") { this.ActiveControl = txtCode; txtCode.Focus(); throw new ArgumentException("La saisie du code est obligatoire!"); } if (txtIntitule.Text == "") { this.ActiveControl = txtIntitule; txtIntitule.Focus(); throw new ArgumentException("La saisie de l'intitulé est obligatoire!"); } using (DbSage db = new DbSage()) { string code = txtCode.Text; if (_mode == Mode.Ajouter && db.F_Departement.Count(item => item.DP_Code == code) > 0) { this.ActiveControl = txtCode; txtCode.Focus(); throw new ArgumentException("Ce code existe déja!"); } F_Departement departement = new F_Departement() { DP_Code = txtCode.Text, DP_intitule = txtIntitule.Text, DP_No = _mode == Mode.Modifier ? _departement.DP_No : 0, }; if (_mode == Mode.Ajouter) { db.F_Departement.Add(departement); } else if (_mode == Mode.Modifier) { db.F_Departement.Add(departement); db.Entry(departement).State = System.Data.Entity.EntityState.Modified; } db.SaveChanges(); }; BindListe(); btNouveau_Click(null, null); } catch (Exception ex) { Program.ErrorMessage(ex.Message); } }