private void Btn_Ajouter_Click(object sender, EventArgs e) { try { if (string.IsNullOrEmpty(Txt_Nom.Text) || string.IsNullOrEmpty(Txt_Code.Text)) { MessageBox.Show("Une ou plusieurs entrées invalides", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { D.PropNom = Txt_Nom.Text; D.PropCode = Txt_Code.Text; Dal_Dept.AddDepartement(D); MessageBox.Show("Ajouté avec succès", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information); foreach (var Txt_Box in this.Controls.OfType <TextBox>()) { Txt_Box.Clear(); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void Btn_Importer_Click(object sender, EventArgs e) { var fileContent = string.Empty; var filePath = string.Empty; using (OpenFileDialog Ofd = new OpenFileDialog() { Filter = "Fichiers Excel | *.xls; *.xlsx; *.xlsm", ValidateNames = true, Multiselect = false }) { if (Ofd.ShowDialog() == DialogResult.OK) { //Get the path of specified file filePath = Ofd.FileName; Dal_Departement Dal_Dept = new Dal_Departement(); List <Departements> DeptList = Dal_Dept.GetAllDepartementsList(); Dal_Ens.GetDataFromExcelFile(filePath, out MydataTabX, out MydataTabY, out MydataTabA, out MydataTabB, out MydataTabC); for (int i = 0; i < MydataTabA.Length; i++) { Departements Dept = new Departements(MydataTabB[i], MydataTabC[i]); if (!DeptList.Contains(Dept)) { Dal_Dept.AddDepartement(Dept); } Enseignants Ens = new Enseignants(MydataTabX[i], MydataTabY[i], MydataTabA[i], "En cours", Dept); Dal_Ens.AddEnseignant(Ens); fillDgvEns(); } MessageBox.Show("Données importées avec succès", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information); /* //Read the contents of the file into a stream * var fileStream = Ofd.OpenFile(); * * using (StreamReader reader = new StreamReader(fileStream)) * { * fileContent = reader.ReadToEnd(); * } */ } } // MessageBox.Show(fileContent, "File Content at path: " + filePath, MessageBoxButtons.OK); }