H
H
Hydra2018-04-08 12:07:34
C++ / C#
Hydra, 2018-04-08 12:07:34

How to populate a DataGridView with elements of an array of class objects?

string[] database = File.ReadAllLines("database.txt"); 
                int len = database.Count(); // Количество строк
                auto[] car = new auto[len]; 
                for (int i = 0; i < len; i++) // Создание элементов массива объектов
                {
                    string line = database[i];
                    string[] linemas = line.Split('|');
                    int id = Convert.ToInt32(linemas[0]);
                    car[i] = new auto(id);
                }

class auto
    {
        public string carname;
        public int i;
        public double price;
        public string address;

        public auto(int _id)
        {
            i = _id - 1; // Индексация в файле начинается с 1
            string[] database = File.ReadAllLines("database.txt"); 
            string line = database[i]; // Строка по айди
            string[] linemas = line.Split('|'); // Разделили строку
            carname = linemas[1];
            price = Convert.ToDouble(linemas[2]);
            address = linemas[3];
        }

        public double CalcRealPrice(double _multiplier)
        {
            return price *= _multiplier;
        }
    }

Now the question itself is, how can you fill the DataGridView, as in the picture, with the created objects when you click on the button (in the event handler)? 5ac9dac20c02b812837879.png
Example from database.txt:
1|Nissan GTR|10000|img\sportcars\car1.jpg
2|Lamborghini Aventador|15000|img\sportcars\car2.jpg

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question