diff --git a/CustomComponents/Classes/Account.cs b/CustomComponents/Classes/Account.cs index 3e201d1..292092a 100644 --- a/CustomComponents/Classes/Account.cs +++ b/CustomComponents/Classes/Account.cs @@ -24,6 +24,7 @@ namespace CustomComponents.Classes Login = login; Town = town; LoginTimes.Add(DateTime.Now); + LoginTimes.Add(DateTime.Today); } } diff --git a/CustomComponents/CustomDataGridView.Designer.cs b/CustomComponents/CustomDataGridView.Designer.cs index e96b498..fe7b9df 100644 --- a/CustomComponents/CustomDataGridView.Designer.cs +++ b/CustomComponents/CustomDataGridView.Designer.cs @@ -34,6 +34,7 @@ // // customGridView // + customGridView.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; customGridView.BackgroundColor = SystemColors.ButtonHighlight; customGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; customGridView.Location = new Point(5, 3); diff --git a/CustomComponents/CustomDataGridView.cs b/CustomComponents/CustomDataGridView.cs index ff562c0..d0ba8d6 100644 --- a/CustomComponents/CustomDataGridView.cs +++ b/CustomComponents/CustomDataGridView.cs @@ -2,6 +2,7 @@ using CustomComponents.Classes; using System; using System.CodeDom; +using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -26,9 +27,9 @@ namespace CustomComponents public void ColumnConfiguration(int columnsQuantity, List headers, List columnWidth, List visibility, List columnName) { - for(int i = 0; i < columnsQuantity; i++) + for (int i = 0; i < columnsQuantity; i++) { - customGridView.Columns.Add(headers.ElementAt(i),columnName.ElementAt(i)); + customGridView.Columns.Add(headers.ElementAt(i), columnName.ElementAt(i)); customGridView.Columns[i].Width = columnWidth.ElementAt(i); customGridView.Columns[i].Visible = visibility.ElementAt(i); } @@ -42,10 +43,9 @@ namespace CustomComponents } set { - if (customGridView.SelectedRows.Count != 0) - { - RowIndex = value; - } + + RowIndex = value; + } } @@ -59,7 +59,7 @@ namespace CustomComponents var type = objects.GetType(); var fields = type.GetProperties(); - foreach(var obj in objects) + foreach (var obj in objects) { List values = new(); values.AddRange(obj.GetType().GetProperties().Select(prop => prop.GetValue(obj))); @@ -67,10 +67,19 @@ namespace CustomComponents DataGridViewRow row = new DataGridViewRow(); row.CreateCells(customGridView); - foreach(DataGridViewColumn column in customGridView.Columns ) + + int columnIndex = 0; + foreach(var value in values) { - row.Cells[column.Index].Value = values.First(); - values.RemoveAt(0); + if (!(value is string) && typeof(IEnumerable).IsAssignableFrom(value.GetType())) + { + row.Cells[columnIndex].Value = EnumerableConverter(value); + } + else + { + row.Cells[columnIndex].Value = value; + } + columnIndex++; } customGridView.Rows.Add(row); } @@ -79,23 +88,49 @@ namespace CustomComponents public T GetAccount() where T : new() { T obj = new T(); - + var props = obj.GetType().GetProperties(); - foreach(var prop in props) + foreach (var prop in props) { - if(customGridView.Columns.Contains(prop.Name)) + if (customGridView.Columns.Contains(prop.Name)) { - prop.SetValue(obj, customGridView.Rows[IndexProperty].Cells[prop.Name].Value); + var elem = customGridView.Rows[IndexProperty].Cells[prop.Name].Value; + if(prop.PropertyType.IsGenericType) + { + prop.SetValue(obj, elem.ToString().Split(";").Select(item => Convert.ToDateTime(item)).ToList()); + continue; + } + prop.SetValue(obj, elem); } } return obj; } + public string EnumerableConverter(object value) + { + + IEnumerable? elements = value as IEnumerable; + + List list = new List(); + foreach (var elem in elements) + { + list.Add(elem.ToString()); + } + + string listAsString = String.Join(";", list); + + return listAsString; + } + private void SelectionChanged(object sender, EventArgs e) { var element = sender as DataGridView; + if (customGridView.SelectedRows.Count == 0) + { + return; + } IndexProperty = element.SelectedRows[0].Index; TaskHandler?.Invoke(this, e); } diff --git a/TestForms/Form1.Designer.cs b/TestForms/Form1.Designer.cs index cb7c1ac..db8d3ab 100644 --- a/TestForms/Form1.Designer.cs +++ b/TestForms/Form1.Designer.cs @@ -36,6 +36,7 @@ buttonConfigureGridView = new Button(); fillGridViewButton = new Button(); getSelectedRowButton = new Button(); + buttonClear = new Button(); SuspendLayout(); // // fillButton @@ -79,12 +80,12 @@ frameCustomGridView1.IndexProperty = 0; frameCustomGridView1.Location = new Point(12, 166); frameCustomGridView1.Name = "frameCustomGridView1"; - frameCustomGridView1.Size = new Size(309, 194); + frameCustomGridView1.Size = new Size(393, 189); frameCustomGridView1.TabIndex = 5; // // buttonConfigureGridView // - buttonConfigureGridView.Location = new Point(327, 182); + buttonConfigureGridView.Location = new Point(411, 180); buttonConfigureGridView.Name = "buttonConfigureGridView"; buttonConfigureGridView.Size = new Size(111, 29); buttonConfigureGridView.TabIndex = 6; @@ -94,7 +95,7 @@ // // fillGridViewButton // - fillGridViewButton.Location = new Point(327, 250); + fillGridViewButton.Location = new Point(411, 248); fillGridViewButton.Name = "fillGridViewButton"; fillGridViewButton.Size = new Size(111, 29); fillGridViewButton.TabIndex = 7; @@ -105,7 +106,7 @@ // // getSelectedRowButton // - getSelectedRowButton.Location = new Point(327, 285); + getSelectedRowButton.Location = new Point(411, 283); getSelectedRowButton.Name = "getSelectedRowButton"; getSelectedRowButton.Size = new Size(111, 29); getSelectedRowButton.TabIndex = 8; @@ -114,11 +115,22 @@ getSelectedRowButton.Visible = false; getSelectedRowButton.Click += getSelectedRowButton_Click; // + // buttonClear + // + buttonClear.Location = new Point(411, 318); + buttonClear.Name = "buttonClear"; + buttonClear.Size = new Size(111, 29); + buttonClear.TabIndex = 9; + buttonClear.Text = "CLEAR"; + buttonClear.UseVisualStyleBackColor = true; + buttonClear.Click += buttonClear_Click; + // // TestFrame // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 451); + Controls.Add(buttonClear); Controls.Add(getSelectedRowButton); Controls.Add(fillGridViewButton); Controls.Add(buttonConfigureGridView); @@ -141,5 +153,6 @@ private Button buttonConfigureGridView; private Button fillGridViewButton; private Button getSelectedRowButton; + private Button buttonClear; } } \ No newline at end of file diff --git a/TestForms/Form1.cs b/TestForms/Form1.cs index 24768d9..63e70c5 100644 --- a/TestForms/Form1.cs +++ b/TestForms/Form1.cs @@ -25,12 +25,13 @@ namespace TestForms { frameCustomGridView1.ColumnConfiguration(5, new List() { "ID", "Login", "LoginTimes", "Town", "Creation" }, - new List() { 50, 50, 50, 50, 50 }, + new List() { 80, 80, 80, 80, 80 }, new List { true, true, true, true, true }, new List { "ID", "Login", "LoginTimes", "Town", "Creation" }); buttonConfigureGridView.Visible = false; fillGridViewButton.Visible = true; getSelectedRowButton.Visible = true; + clearButton.Visible = true; } private void fillGridViewButton_Click(object sender, EventArgs e) @@ -51,11 +52,21 @@ namespace TestForms StringBuilder sb = new StringBuilder(); - foreach(var prop in properties) + foreach (var prop in properties) { + if(prop.PropertyType.IsGenericType) + { + sb.Append(($"{prop.Name}: {frameCustomGridView1.EnumerableConverter(prop.GetValue(obj))}\n")); + continue; + } sb.Append($"{prop.Name}: {prop.GetValue(obj)}\n"); } - MessageBox.Show( sb.ToString() ); + MessageBox.Show(sb.ToString()); + } + + private void buttonClear_Click(object sender, EventArgs e) + { + frameCustomGridView1.ClearRows(); } } } \ No newline at end of file