J'essaie de vouloir importer plusieurs fichiers au format txt et de les afficher dans un datagridview en C #, je n'arrive qu'à importer un seul fichier au lieu de plusieurs fichiers. voici mon code: j'ai deux classes pour connaître form1 et Helper

le fichier Form
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
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.IO;
 
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataTable table = new DataTable();
 
        private void Form1_Load(object sender, EventArgs e)
        {
 
 
        }
 
        private void btn_import_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog
            {
                Title = "Parcourir le fichier",
                CheckFileExists = true,
                CheckPathExists = true,
                DefaultExt = "txt",
                Filter = "txt files (*.txt)|*.txt",
                FilterIndex = 2,
                RestoreDirectory = true,
                ReadOnlyChecked=true,ShowReadOnly=true,
            };
 
            if (openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                foreach (string file in openFileDialog1.FileNames)
                {
                    textBox1.Text = openFileDialog1.FileName;
                    Helper.file = textBox1.Text;
                }
 
                dataGridView1.DataSource = Helper.DataTableFromTextFile(textBox1.Text);
            }
        }
 
        private void label1_Click(object sender, EventArgs e)
        {
 
        }
    }
}
voici le fichier Helper

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.IO;
 
namespace WindowsFormsApp1
{
    class Helper
    {
        public static string file;
        public static DataTable DataTableFromTextFile(string location,char delimiter='\t')
        {
            DataTable result;
            location = file;
            string[] linearay = File.ReadAllLines(location);
 
            result = FromDataTable(linearay, delimiter);
            return result;
        }
 
        private static DataTable FromDataTable(string[] linearay, char delimiter)
        {
            DataTable dt = new DataTable();
            AddCoulumnToTable(linearay, delimiter,ref dt);
            AddRowToTable(linearay, delimiter, ref dt);
            return dt;
        }
 
        private static void AddRowToTable(string[] Values, char delimiter, ref DataTable dt)
        {
            for (int i = 0; i < Values.Length; i++)
            {
                string[] valeurs = Values[i].Split(delimiter);
                DataRow dr = dt.NewRow();
                for (int j = 0; j < valeurs.Length; j++)
                {
                    dr[j] = valeurs[j];
                }
                dt.Rows.Add(dr);
            }
        }
 
        private static void AddCoulumnToTable(string[] columnCollectioi, char delimiter, ref DataTable dt)
        {
            string[] colones = columnCollectioi[0].Split(delimiter);
            foreach (string columnName  in colones)
            {
                DataColumn dc = new DataColumn();
                dt.Columns.Add(dc);
            }
        }
    }
}

voici les images de différents fichiers txt
Nom : EcTPf.png
Affichages : 144
Taille : 35,6 Ko
Nom : IXWys.png
Affichages : 161
Taille : 35,7 Ko
Nom : jA7gU.png
Affichages : 152
Taille : 35,7 Ko