Add objects.

This commit is contained in:
ElEgEv 2023-09-27 11:26:30 +04:00
parent 32b483ff02
commit 78810b76e6
2 changed files with 13 additions and 6 deletions

View File

@ -8,7 +8,7 @@ namespace VisualComponentsForm
private int numberObject;
private List<string> nameCol = new List<string> { "Íîìåð", "Èìÿ", "Ôàìèëèÿ", "Âîçðàñò" };
private List<int> widthCol = new List<int> { 30, 30, 30, 30 };
private List<int> widthCol = new List<int> { 60, 60, 60, 60 };
private List<bool> showCol = new List<bool> { true, true, true, true };
private List<string> nameField = new List<string> { "Id", "Name", "Surname", "Age" };
private List<People> peoples;
@ -20,9 +20,9 @@ namespace VisualComponentsForm
peoples = new List<People>
{
new People(0, "Íèêèòà", "Ñåðãååâ", 20),
new People(0, "Äàðüÿ", "Áàëáåðîâà", 20),
new People(0, "Äàíèë", "Ìàëèí", 20),
new People(0, "Íèêîëàé", "Ðàñïàåâ", 20)
new People(1, "Äàðüÿ", "Áàëáåðîâà", 19),
new People(2, "Äàíèë", "Ìàëèí", 25),
new People(3, "Íèêîëàé", "Ðàñïàåâ", 23)
};
InitializeComponent();
@ -81,6 +81,12 @@ namespace VisualComponentsForm
myDataGridView.AddObject(peoples[numberObject]);
numberObject++;
if (numberObject == 4)
{
numberObject = 0;
}
}
}
}

View File

@ -38,12 +38,13 @@ namespace VisualComponentsLib
//публичный параметризованный метод для добавления нового объекта в список
public void AddObject<T>(T newObject)
{
DataGridViewRow row = new DataGridViewRow();
DataGridViewRow row = (DataGridViewRow)dataGridView.Rows[0].Clone();
foreach (var prop in newObject.GetType().GetProperties())
{
object value = prop.GetValue(newObject);
row.Cells[prop.Name].Value = value;
row.Cells[dataGridView.Columns[prop.Name].Index].Value = value;
}
dataGridView.Rows.Add(row);