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