AddList fix

This commit is contained in:
DavidMakarov 2024-10-04 06:48:52 +04:00
parent 38db4c11d2
commit 7895adaa07

View File

@ -52,9 +52,22 @@ namespace Components
dataGridViewTable.Rows.Clear(); dataGridViewTable.Rows.Clear();
} }
public void AddObjectList(List<object> os) public void AddList<T>(List<T> values)
{ {
dataGridViewTable.DataSource = os ?? throw new ArgumentNullException(); ClearRows();
var props = typeof(T).GetProperties();
foreach (T value in values)
{
int newRowInd = dataGridViewTable.Rows.Add(value);
foreach (DataGridViewColumn col in dataGridViewTable.Columns)
{
var prop = props.FirstOrDefault(x => x.Name == col.Name)
?? throw new InvalidOperationException($"No property {col.Name} found in type {typeof(T).Name}");
var val = prop.GetValue(value);
dataGridViewTable.Rows[newRowInd].Cells[col.Index].Value = val;
}
}
} }
public T GetObjectFromRow<T>() public T GetObjectFromRow<T>()
@ -79,23 +92,5 @@ namespace Components
} }
return returnObject; return returnObject;
} }
public void AddList<T> (List<T> values)
{
ClearRows();
var props = typeof(T).GetProperties();
foreach (T value in values)
{
int newRowInd = dataGridViewTable.Rows.Add(value);
foreach (DataGridViewColumn col in dataGridViewTable.Columns)
{
var prop = props.FirstOrDefault(x => x.Name == col.Name)
?? throw new InvalidOperationException($"No property {col.Name} found in type {typeof(T).Name}");
var val = prop.GetValue(value);
dataGridViewTable.Rows[newRowInd].Cells[col.Index].Value = val;
}
}
}
} }
} }