How to load data into a Windows forms app?

How to load data into a Windows forms app?
typescript
Ethan Jackson

Is there any easy way to load .csv data in a C# console app? The app loads csv data and calculate with them.

This is the class:

class Berles { public int Uid { get; set; } public int PaintingId { get; set; } public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } public int DailyPrice { get; set; } public string Artist { get; set; } public string Title { get; set; } public int Duration => (EndDate - StartDate).Days; public int TotalPrice => Duration * DailyPrice; }

Grabbing data from the .CSV file:

List<Berles> berlesek = File.ReadAllLines("festmeny_berlesek_2024.csv") .Skip(1) .Select(sor => { var t = sor.Split(','); return new Berles { Uid = int.Parse(t[0]), PaintingId = int.Parse(t[1]), StartDate = DateTime.Parse(t[2]), EndDate = DateTime.Parse(t[3]), DailyPrice = int.Parse(t[4]), Artist = t[5], Title = t[6] }; }) .ToList();

Thank you!

Answer

List<Berles> ReadCsv(string path) { var list = new List<Berles>(); using (var reader = new StreamReader(path)) { string? line; bool isFirstLine = true; while ((line = reader.ReadLine()) != null) { if (isFirstLine) { isFirstLine = false; continue; } var fields = line.Split(','); var berles = new Berles { Uid = int.Parse(fields[0]), PaintingId = int.Parse(fields[1]), StartDate = DateTime.ParseExact(fields[2]), EndDate = DateTime.ParseExact(fields[3]), DailyPrice = int.Parse(fields[4]), Artist = fields[5], Title = fields[6] }; list.Add(berles); } } return list; } }

Please try this...

Related Articles