Partage
  • Partager sur Facebook
  • Partager sur Twitter

Importation fichiers txt avec bar de progression

Importer plusieurs fichiers dans un datagridview avec progressbar

    18 janvier 2021 à 14:31:50

    Salut les amis, 

    j'ai un code qui importer plusieurs fichiers txt vers un datagridview, j'aimerais à ce que l'operation se fasse avec une bar de progression qui va indiquer à l'utilisateur l'evolution de l'opération, si quelqu'un à une solution qui peut m'aider

    voici le code :

    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 void AddTextFileContentToDataTable(string finename, DataTable dt, char delimiter)
            {
                string[] lines = File.ReadAllLines(finename);
                if (dt.Columns.Count == 0)
                {
                    AddCoulumnToTable(lines, delimiter, ref dt);
                }
                AddRowToTable(lines, delimiter, ref dt);
            }
     
          
     
            private static void AddRowToTable(string[] Values, char delimiter, ref DataTable dt)
            {
                for (int i = 2; 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);
                }
            }
        }
    }
    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();
            }
            
     
            private void Form1_Load(object sender, EventArgs e)
            {
     
                dataGridView1.DataSource = new DataTable();
            }
     
            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,
                    Multiselect = true,
                    RestoreDirectory = true,
                    ReadOnlyChecked = true,
                    ShowReadOnly = true,
                };
                char c = '\t';
                
     
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    foreach (string file in openFileDialog1.FileNames)
                    {
                        textBox1.Text = file;
                        Helper.file = file;
                        Helper.AddTextFileContentToDataTable(textBox1.Text, (DataTable)dataGridView1.DataSource, c);
     
     
                    }
                    // dataGridView1.DataSource = Helper.DataTableFromTextFile(textBox1.Text);
     
                   // Helper.AddTextFileContentToDataTable(textBox1.Text, (DataTable)dataGridView1.DataSource, c);
     
     
     
                }
     
            }
            
            private void label1_Click(object sender, EventArgs e)
            {
     
            }
     
        }
    }

    Merci d'avance pour vos intervention!!!!

    • Partager sur Facebook
    • Partager sur Twitter
      18 janvier 2021 à 15:46:51

      Que reprochez-vous à l'exemple de la ProgressBar dans la documentation M$ ?

      https://docs.microsoft.com/fr-fr/dotnet/api/system.windows.forms.progressbar?view=net-5.0

      • Partager sur Facebook
      • Partager sur Twitter
      Je recherche un CDI/CDD/mission freelance comme Architecte Logiciel/ Expert Technique sur technologies Microsoft.

      Importation fichiers txt avec bar de progression

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown