From 04c84290e617ad9fab5c174b1c195b6d0518eb90 Mon Sep 17 00:00:00 2001 From: Yourdax Date: Wed, 18 Sep 2024 19:14:12 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B2=D1=80=D0=BE=D0=B4=D0=B5=20done?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- KopLab1/FormLibrary/ValueTableControl.cs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/KopLab1/FormLibrary/ValueTableControl.cs b/KopLab1/FormLibrary/ValueTableControl.cs index 778c3e6..2e582b1 100644 --- a/KopLab1/FormLibrary/ValueTableControl.cs +++ b/KopLab1/FormLibrary/ValueTableControl.cs @@ -52,7 +52,7 @@ namespace FormLibrary public int SelectedRowIndex { - get => dataGridView1.SelectedRows.Count > 0 ? dataGridView1.SelectedRows[0].Index : -1; + get => dataGridView1.SelectedRows[0].Index; set { if (value >= 0 && value < dataGridView1.Rows.Count) @@ -74,7 +74,7 @@ namespace FormLibrary foreach (DataGridViewColumn column in dataGridView1.Columns) { var prop = typeof(T).GetProperty(column.DataPropertyName); - if (prop != null && prop.CanWrite) + if (prop != null) { var value = selectedRow.Cells[column.Index].Value; prop.SetValue(obj, Convert.ChangeType(value, prop.PropertyType)); @@ -84,15 +84,24 @@ namespace FormLibrary return obj; } - public void FillData(List students) + public void FillData(List objects) { - dataGridView1.DataSource = null; dataGridView1.Rows.Clear(); - foreach (var student in students) + if (objects == null || !objects.Any()) { - dataGridView1.Rows.Add(student.Group, student.FullName, student.Course); + return; + } + + var properties = typeof(T).GetProperties(); + + foreach (var obj in objects) + { + var values = properties.Select(p => p.GetValue(obj, null)).ToArray(); + dataGridView1.Rows.Add(values); } } + + } }