AddList fix

This commit is contained in:
DavidMakarov 2024-10-04 06:48:52 +04:00
parent 9863bdba24
commit 8db45c48bf

View File

@ -52,9 +52,22 @@ namespace WinFormsLibrary1
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>()
@ -79,23 +92,5 @@ namespace WinFormsLibrary1
}
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;
}
}
}
}
}