using System; using System.Collections.Generic; using System.Linq; using…
- User
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms;
namespace TP_Evaluation_Forms_IMBAUD_Florian { public partial class FormAjouterEnregistrement : Form { private string tableName; private Dictionary<string, Control> inputControls = new Dictionary<string, Control>();
private List<object> modificationsEnAttente = new List<object>(); public FormAjouterEnregistrement(string tableName) { InitializeComponent(); this.tableName = tableName; this.Text = $"Ajouter un enregistrement dans {tableName}"; GenerateFormFields(); } private void GenerateFormFields() { using (var contexte = new AppDbContext()) { switch (tableName) { case "Departements": AddField(panneauChampsARemplir, "department_name", new TextBox(), "Nom Département"); break; case "Employes": AddField(panneauChampsARemplir, "first_name", new TextBox(), "Prénom"); AddField(panneauChampsARemplir, "last_name", new TextBox(), "Nom"); AddField(panneauChampsARemplir, "hire_date", new DateTimePicker(), "Date d'Embauche"); AddField(panneauChampsARemplir, "department_id", new TextBox(), "ID Département"); AddField(panneauChampsARemplir, "salary", new TextBox(), "Salaire"); break; case "EmployesProjets": AddField(panneauChampsARemplir, "employee_id", new TextBox(), "ID Employé"); AddField(panneauChampsARemplir, "project_id", new TextBox(), "ID Projet"); AddField(panneauChampsARemplir, "role", new TextBox(), "Rôle"); break; case "Projets": AddField(panneauChampsARemplir, "project_name", new TextBox(), "Nom Projet"); AddField(panneauChampsARemplir, "start_date", new DateTimePicker(), "Date de Début"); AddField(panneauChampsARemplir, "end_date", new DateTimePicker(), "Date de Fin"); break; default: MessageBox.Show("Table non reconnue.", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error); this.Close(); break; } } // Ajouter le panel au formulaire Controls.Add(panneauChampsARemplir); } private void AddField(TableLayoutPanel panel, string propertyName, Control control, string labelText) { int row = panel.RowCount++; panel.RowStyles.Add(new RowStyle(SizeType.AutoSize)); Label label = new Label { Text = labelText, Anchor = AnchorStyles.Right, AutoSize = true }; panel.Controls.Add(label, 0, row); control.Anchor = AnchorStyles.Left | AnchorStyles.Right; panel.Controls.Add(control, 1, row); inputControls.Add(propertyName, control); } private void BtnEnregistrer_Click(object sender, EventArgs e) { switch (tableName) { case "Departements": var nouveauDept = new CDepartement { department_name = GetControlValue("department_name") }; modificationsEnAttente.Add(nouveauDept); break; case "Employes": var nouvelEmploye = new CEmploye { first_name = GetControlValue("first_name"), last_name = GetControlValue("last_name"), hire_date = ((DateTimePicker)inputControls["hire_date"]).Value, department_id = string.IsNullOrWhiteSpace(GetControlValue("department_id")) ? (int?)null : int.Parse(GetControlValue("department_id")), salary = decimal.Parse(GetControlValue("salary")) }; modificationsEnAttente.Add(nouvelEmploye); break; case "EmployesProjets": var nouvelEmployeProjet = new CEmployeProjet { employee_id = int.Parse(GetControlValue("employee_id")), project_id = int.Parse(GetControlValue("project_id")), role = GetControlValue("role") }; modificationsEnAttente.Add(nouvelEmployeProjet); break; case "Projets": var nouveauProjet = new CProjet { project_name = GetControlValue("project_name"), start_date = ((DateTimePicker)inputControls["start_date"]).Value, end_date = ((DateTimePicker)inputControls["end_date"]).Value }; modificationsEnAttente.Add(nouveauProjet); break; default: MessageBox.Show("Table non reconnue.", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } MessageBox.Show("Enregistrement ajouté avec succès à la liste temporaire.", "Succès", MessageBoxButtons.OK, MessageBoxIcon.Information); DialogResult = DialogResult.OK; Close(); } private string GetControlValue(string propertyName) { if (inputControls.TryGetValue(propertyName, out Control control)) { if (control is TextBox txt) return txt.Text.Trim(); } return string.Empty; } private void btnAnnuler_Click(object sender, EventArgs e) { Close(); } } - Assistant