From 7f404cadd463986c05d851bda22e851f9834c000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=9A=D1=80?= =?UTF-8?q?=D1=8E=D0=BA=D0=BE=D0=B2?= Date: Tue, 22 Oct 2024 21:30:06 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BC=D0=BF=D0=BE=D0=BD=D0=B5=D0=BD=D1=82,=20?= =?UTF-8?q?=D1=87=D1=82=D0=BE=D0=B1=D1=8B=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=D0=BB=D0=BE=20=D0=B1=D0=B5=D0=B7=20=D0=BE=D1=88=D0=B8?= =?UTF-8?q?=D0=B1=D0=BE=D0=BA=20=D0=B8=20=D0=BF=D0=BE=20=D1=82=D0=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- COP/KryukovLib/CustomDataGridView.Designer.cs | 1 + COP/KryukovLib/CustomDataGridView.cs | 23 +++++++++++++++---- COP/Test/Form1.cs | 3 ++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/COP/KryukovLib/CustomDataGridView.Designer.cs b/COP/KryukovLib/CustomDataGridView.Designer.cs index 7bd7ba9..6134fd6 100644 --- a/COP/KryukovLib/CustomDataGridView.Designer.cs +++ b/COP/KryukovLib/CustomDataGridView.Designer.cs @@ -41,6 +41,7 @@ dataGridViewItems.Name = "dataGridViewItems"; dataGridViewItems.RowHeadersWidth = 51; dataGridViewItems.RowTemplate.Height = 29; + dataGridViewItems.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridViewItems.Size = new Size(583, 353); dataGridViewItems.TabIndex = 0; // diff --git a/COP/KryukovLib/CustomDataGridView.cs b/COP/KryukovLib/CustomDataGridView.cs index 510591d..806631c 100644 --- a/COP/KryukovLib/CustomDataGridView.cs +++ b/COP/KryukovLib/CustomDataGridView.cs @@ -57,28 +57,43 @@ namespace KryukovLib T val = new(); var propertiesObj = typeof(T).GetProperties(); - foreach (var properties in propertiesObj) + foreach (var property in propertiesObj) { bool propIsExist = false; int columnIndex = 0; for (; columnIndex < dataGridViewItems.Columns.Count; columnIndex++) { - if (dataGridViewItems.Columns[columnIndex].DataPropertyName.ToString() == properties.Name) + if (dataGridViewItems.Columns[columnIndex].DataPropertyName.ToString() == property.Name) { propIsExist = true; break; } } + if (propIsExist) { object value = dataGridViewItems.SelectedRows[0].Cells[columnIndex].Value; - properties.SetValue(val, Convert.ChangeType(value, properties?.PropertyType)); - }; + + Type propertyType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType; + + if (value == null || value == DBNull.Value) + { + if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) + { + property.SetValue(val, null); + } + } + else + { + property.SetValue(val, Convert.ChangeType(value, propertyType)); + } + } } return val; } + public void AddItem(T item, int RowIndex) where T : class { if (item == null) diff --git a/COP/Test/Form1.cs b/COP/Test/Form1.cs index 6a034ed..0a0fedd 100644 --- a/COP/Test/Form1.cs +++ b/COP/Test/Form1.cs @@ -153,6 +153,7 @@ namespace Test { (sender as Control).BackColor = Color.White; var rnd = new Random(); + var list2d = new List<(string Name, double value)>() { ("Series 2", rnd.Next()), ("Series 3", rnd.Next()), ("Series 52", rnd.Next()) }; excelGistogram1.CreateDoc(new ChartConfig { FilePath = "bar.xlsx", @@ -161,7 +162,7 @@ namespace Test LegendLocation = KryukovLib.Models.Location.Top, Data = new Dictionary> { - { "Series 1", new() { ("Series 2", rnd.Next()), ("Series 3", rnd.Next()), ("Series 52", rnd.Next()) } } + { "Series 1", list2d } } }); (sender as Control).BackColor = Color.Green;