From a46bc0427dd059c03b343a6e1b2e7d2fbb8e99e6 Mon Sep 17 00:00:00 2001 From: Blauschleim Date: Mon, 16 May 2022 18:35:45 +0200 Subject: [PATCH] merges export functions, removed the option for users to add or delete rows --- Form1.Designer.cs | 28 +++++++++++++++++--- Form1.cs | 65 ++++++++++++++--------------------------------- Form1.resx | 6 +++++ 3 files changed, 49 insertions(+), 50 deletions(-) diff --git a/Form1.Designer.cs b/Form1.Designer.cs index 55e4c66..dbce63a 100644 --- a/Form1.Designer.cs +++ b/Form1.Designer.cs @@ -39,6 +39,8 @@ namespace it_projekt this.Id = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.firstname = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.lastname = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.Department_Short = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.Department = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.creationDate = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.markItem = new System.Windows.Forms.DataGridViewCheckBoxColumn(); this.jsonRBtn = new System.Windows.Forms.RadioButton(); @@ -100,18 +102,22 @@ namespace it_projekt // // dataGrid // + this.dataGrid.AllowUserToAddRows = false; + this.dataGrid.AllowUserToDeleteRows = false; this.dataGrid.AllowUserToOrderColumns = true; this.dataGrid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGrid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.Id, this.firstname, this.lastname, + this.Department_Short, + this.Department, this.creationDate, this.markItem}); this.dataGrid.Location = new System.Drawing.Point(12, 109); this.dataGrid.Name = "dataGrid"; this.dataGrid.RowTemplate.Height = 25; - this.dataGrid.Size = new System.Drawing.Size(543, 225); + this.dataGrid.Size = new System.Drawing.Size(743, 225); this.dataGrid.TabIndex = 7; this.dataGrid.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGrid_CellContentClick); // @@ -133,6 +139,18 @@ namespace it_projekt this.lastname.Name = "lastname"; this.lastname.ReadOnly = true; // + // Department_Short + // + this.Department_Short.HeaderText = "Abteilungs Kürzel"; + this.Department_Short.Name = "Department_Short"; + this.Department_Short.ReadOnly = true; + // + // Department + // + this.Department.HeaderText = "Abteilung"; + this.Department.Name = "Department"; + this.Department.ReadOnly = true; + // // creationDate // this.creationDate.HeaderText = "Erstellungssdatum"; @@ -172,7 +190,7 @@ namespace it_projekt // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(559, 336); + this.ClientSize = new System.Drawing.Size(758, 336); this.Controls.Add(this.xmlRBtn); this.Controls.Add(this.jsonRBtn); this.Controls.Add(this.dataGrid); @@ -200,13 +218,15 @@ namespace it_projekt private System.Windows.Forms.TextBox userTxt; private System.Windows.Forms.TextBox passTxt; private System.Windows.Forms.DataGridView dataGrid; + private System.Windows.Forms.RadioButton jsonRBtn; + private System.Windows.Forms.RadioButton xmlRBtn; private System.Windows.Forms.DataGridViewTextBoxColumn Id; private System.Windows.Forms.DataGridViewTextBoxColumn firstname; private System.Windows.Forms.DataGridViewTextBoxColumn lastname; + private System.Windows.Forms.DataGridViewTextBoxColumn Department_Short; + private System.Windows.Forms.DataGridViewTextBoxColumn Department; private System.Windows.Forms.DataGridViewTextBoxColumn creationDate; private System.Windows.Forms.DataGridViewCheckBoxColumn markItem; - private System.Windows.Forms.RadioButton jsonRBtn; - private System.Windows.Forms.RadioButton xmlRBtn; } } diff --git a/Form1.cs b/Form1.cs index c424c8c..0ed11e6 100644 --- a/Form1.cs +++ b/Form1.cs @@ -22,9 +22,9 @@ namespace it_projekt private void exportBtn_Click(object sender, EventArgs e) { if (jsonRBtn.Checked) - exportJSON(); + export("All files (*.*)|*.*|json files (*.json)|*.json", "benutzer.json"); else - exportXML(); + export("All files (*.*)|*.*|xml files (*.xml)|*.xml", "benutzer.xml"); } private void loginBtn_Click(object sender, EventArgs e) @@ -52,19 +52,17 @@ namespace it_projekt private void dataGrid_CellContentClick(object sender, DataGridViewCellEventArgs e) { - + } - private void exportJSON() + private void export(string Filter, string FileName) { Stream myStream; SaveFileDialog saveFileDialog1 = new SaveFileDialog(); - - saveFileDialog1.Filter = "All files (*.*)|*.*|json files (*.json)|*.json"; + saveFileDialog1.Filter = Filter; saveFileDialog1.FilterIndex = 2; saveFileDialog1.RestoreDirectory = true; - saveFileDialog1.FileName = "benutzer.json"; - + saveFileDialog1.FileName = FileName; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { if ((myStream = saveFileDialog1.OpenFile()) != null) @@ -72,59 +70,34 @@ namespace it_projekt List markedUsers = new List(); foreach (DataGridViewRow row in dataGrid.Rows) { - if (row.Cells[4].Value != null) + if (row.Cells[6].Value != null) { - if (row.Cells[4].Value.ToString() == "True") + if (row.Cells[6].Value.ToString() == "True") { markedUsers.Add(db.Persons.Find(person => person.Id == row.Cells[0].Value.ToString())); } } } - string json = JsonSerializer.Serialize(markedUsers); StreamWriter sw = new StreamWriter(myStream); - sw.Write(json); - sw.Flush(); - sw.Close(); - myStream.Close(); - } - } - } - - private void exportXML() - { - Stream myStream; - SaveFileDialog saveFileDialog1 = new SaveFileDialog(); - - saveFileDialog1.Filter = "All files (*.*)|*.*|xml files (*.xml)|*.xml"; - saveFileDialog1.FilterIndex = 2; - saveFileDialog1.RestoreDirectory = true; - saveFileDialog1.FileName = "benutzer.xml"; - - if (saveFileDialog1.ShowDialog() == DialogResult.OK) - { - if ((myStream = saveFileDialog1.OpenFile()) != null) - { - List markedUsers = new List(); - foreach (DataGridViewRow row in dataGrid.Rows) + if (FileName.Contains("xml")) { - if (row.Cells[4].Value != null) - { - if (row.Cells[4].Value.ToString() == "True") - { - markedUsers.Add(db.Persons.Find(person => person.Id == row.Cells[0].Value.ToString())); - } - } + 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); } - StreamWriter sw = new StreamWriter(myStream); - var writer = new System.Xml.Serialization.XmlSerializer(typeof(List)); - writer.Serialize(sw, markedUsers); sw.Flush(); sw.Close(); myStream.Close(); } + } } + private void Form1_Load(object sender, EventArgs e) { @@ -137,7 +110,7 @@ namespace it_projekt private void xmlRBtn_CheckedChanged(object sender, EventArgs e) { - + } } } diff --git a/Form1.resx b/Form1.resx index 1294763..1715578 100644 --- a/Form1.resx +++ b/Form1.resx @@ -66,6 +66,12 @@ True + + True + + + True + True