Bonjour à tous,
je vous novice dans le C# donc soyez indulgent
Je commence un petit logiciel pour ma compta perso. Je récupère le relevé bancaire de ma banque en CSV, je lis chaque ligne du CSV et j'intègre les données au fur et à mesure dans une liste.
J'ai un petit soucis lors de l'écriture dans ma liste. Si ma liste contient 5 enregistrements, ceux-ci ont la même valeur (celle de la dernière ligne CSV lue).
Un exemple pour mieux comprendre (capture espion express de visual studio) :
le premier enregistrement lu (CSV) et écris dans la liste : tout va bien pour l'instant !!
- maCompta Count = 1 System.Collections.Generic.List<WindowsFormsApp1.ComptaCSV>
- [0] {WindowsFormsApp1.ComptaCSV} WindowsFormsApp1.ComptaCSV
Date "29/01/2020" string
Libelle "\"ACHAT 28.01.20"" string
Montant_euros -10 decimal
Deuxième enregistrement lu et écris dans la liste : le premier enregistrement se réinitialise avec les données du dernier enregistrement
- maCompta Count = 2 System.Collections.Generic.List<WindowsFormsApp1.ComptaCSV>
- [0] {WindowsFormsApp1.ComptaCSV} WindowsFormsApp1.ComptaCSV
Date "29/01/2020" string
Libelle "\"VIREMENT REFERENCE : 7855\"" string
Montant_euros 20.00 decimal
- [1] {WindowsFormsApp1.ComptaCSV} WindowsFormsApp1.ComptaCSV
Date "29/01/2020" string
Libelle "\"VIREMENT REFERENCE : 7855\"" string
Montant_euros 20.00 decimal
Pareil pour le 3ème enregistrement lu, le 4ème ...
- maCompta Count = 3 System.Collections.Generic.List<WindowsFormsApp1.ComptaCSV>
- [0] {WindowsFormsApp1.ComptaCSV} WindowsFormsApp1.ComptaCSV
Date "27/01/2020" string
Libelle "\"PRELEVEMENT DE 00011000000 A03 C\"" string
Montant_euros -6.64 decimal
- [1] {WindowsFormsApp1.ComptaCSV} WindowsFormsApp1.ComptaCSV
Date "27/01/2020" string
Libelle "\"PRELEVEMENT DE 00011000000 A03 C\"" string
Montant_euros -6.64 decimal
- [2] {WindowsFormsApp1.ComptaCSV} WindowsFormsApp1.ComptaCSV
Date "27/01/2020" string
Libelle "\"PRELEVEMENT DE 00011000000 A03 C\"" string
Montant_euros -6.64 decimal
Mon code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SQLite; using System.IO; namespace WindowsFormsApp1 { public partial class Form1 : Form { string dbPath = "compta.db"; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // on affiche la table SQLiteConnection connection = new SQLiteConnection("DataSource=" + dbPath); connection.Open(); SQLiteCommand command = new SQLiteCommand(connection); command.CommandText = "SELECT * from compte"; using (SQLiteDataReader sdr = command.ExecuteReader()) { //create a new Database DataTable dt = new DataTable(); //LOAD DATAREADER INTO THE DATATABLE dt.Load(sdr); sdr.Close(); connection.Close(); dataGridView1.DataSource = dt; } } private void PARCOURIR_Click(object sender, EventArgs e) { OpenFileDialog openFile = new OpenFileDialog(); openFile.DefaultExt = "csv"; openFile.Filter = "Fichier CSV (*.csv)|*.csv"; openFile.Multiselect = false; openFile.ShowDialog(); if (openFile.FileName.Length > 0) { StreamReader reader = new StreamReader(File.OpenRead(openFile.FileName)); List<ComptaCSV> maCompta = new List<ComptaCSV>(); ComptaCSV laBanque = new ComptaCSV(); while (!reader.EndOfStream) { string line = reader.ReadLine(); if (!String.IsNullOrWhiteSpace(line)) { string[] values = line.Split(';'); if (values[0] != "Date") { if (values.Length >= 4) { laBanque.Date = values[0]; laBanque.Libelle = values[1]; laBanque.Montant_euros = Convert.ToDecimal(values[2]); maCompta.Add(laBanquePostale); } } } } reader.Close(); } } } public class ComptaCSV { public string Date { get; set; } public string Libelle { get; set; } public decimal Montant_euros { get; set; } } }
Je n'arrive pas à comprendre.
Si c'est plus clair pour vous, quel est le truc qui manque ou mis en trop, pour que chaque enregistrement de la liste corresponde bien à chaque ligne du CSV ?
Merci pour votre aide.
Partager