using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Text.Json; using System.Windows.Forms; namespace it_projekt { public partial class Form1 : Form { private Database db = null; public Form1() { InitializeComponent(); this.exportBtn.Enabled = false; } private void exportBtn_Click(object sender, EventArgs e) { if (jsonRBtn.Checked) export("All files (*.*)|*.*|json files (*.json)|*.json", "benutzer.json"); else export("All files (*.*)|*.*|xml files (*.xml)|*.xml", "benutzer.xml"); } public void LoadUsersFromTable(object sender, EventArgs e) { db = Database.getDatabase(this.userTxt.Text, this.passTxt.Text); if (db.Persons == null) { Database.resetDatabase(); MessageBox.Show("Verbindung mit der Datenbank fehlgeschlagen :( \nVersuchen Sie es erneut.", "Datenbank Fehler!"); } else { foreach (Person person in db.Persons) { this.dataGrid.Rows.Add(person.Id, person.Firstname, person.Lastname, person.Department_short, person.Department_long, person.CreationDate, false); } this.loginBtn.Enabled = false; } } private void export(string Filter, string FileName) { List markedUsers = new List(); foreach (DataGridViewRow row in dataGrid.Rows) { if (Convert.ToBoolean(row.Cells[6].Value)) { markedUsers.Add(db.Persons.Find(person => person.Id == row.Cells[0].Value.ToString())); } } if (markedUsers.Count == 0) { MessageBox.Show("Bitte wählen Sie mindestens eine Person zum exportieren aus!", "Exporter Error!"); return; } Stream myStream; SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = Filter; saveFileDialog1.FilterIndex = 2; saveFileDialog1.RestoreDirectory = true; saveFileDialog1.FileName = FileName; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { if ((myStream = saveFileDialog1.OpenFile()) != null) { StreamWriter sw = new StreamWriter(myStream); if (FileName.Contains("xml")) { var writer = new System.Xml.Serialization.XmlSerializer(typeof(List)); writer.Serialize(sw, markedUsers); } else //If xml is not selected, json is the only other option { string json = JsonSerializer.Serialize(markedUsers); sw.Write(json); } sw.Flush(); sw.Close(); myStream.Close(); } } } private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e) { //Check to ensure that the row CheckBox is clicked. if (e.RowIndex >= 0 && e.ColumnIndex == 6) { //Reference the GridView Row. DataGridViewRow row = dataGrid.Rows[e.RowIndex]; //Set the CheckBox selection. row.Cells[6].Value = Convert.ToBoolean(row.Cells[6].EditedFormattedValue); exportBtn.Enabled = false; //If CheckBox is checked, display Message Box. if (Convert.ToBoolean(row.Cells[6].Value)) { exportBtn.Enabled = true; } foreach (DataGridViewRow r in dataGrid.Rows) { if (Convert.ToBoolean(r.Cells[6].Value)) { exportBtn.Enabled = true; } } } } } }