Question Imports multiple txt files into datagriview

Kams

New member
Joined
Jan 13, 2021
Messages
3
Programming Experience
Beginner
Hi I have as part of an exercise several txt files, I look for a comment Once imported and display them in A datagriview to then register them in the database.
The different files have the same configuration I have to import during import the first line of each file.
Thank you
 
Last edited by a moderator:
Okay. You told us what you need to do. What problem are you encountering?
 
The grid is not really relevant. You can certainly display the data in a grid before saving but it is not really part of the process. If the user is to make any modifications to the data before saving, they can also do so via the grid.

What you ought to do is to read the data into a DataTable, which you can then bind to a DataGridView via a BindingSource. You can then save the data to your database using a data adapter.

As for reading the data, there are different ways that could be done. You could use an OleDbDataAdapter or OleDbDataReader to populate the DataTable directly or you could use a TextFieldParser or some third-party component to read the data and then populate the DataTable manually.

I suggest that you do a bit more research and then get back to us if and when you have more specific questions.
 
When I edited your first post, I stated that only posts in English are acceptable and that, while I had translated that post for you, any further non-English posts would be deleted. As such, your second post has been deleted. Please post only in English.
 
Ok I have two classes namely form1 and Helper I meet you the code the problem is I can only import one file instead of multiple files. Form code:
Form:
 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; WindowsFormsApp1 namespace {public partial class Form1: Form {public Form1 () {InitializeComponent (); } Table DataTable = new DataTable (); private void Form1_Load (object sender, EventArgs e) {} private void btn_import_Click (object sender, EventArgs e) {OpenFileDialog openFileDialog1 = new OpenFileDialog {Title = "Browse file", CheckFileExists = true, CheckPathExists = true , DefaultExt ", 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 empty label1_Click (object sender, EventArgs e) {}}} [/ CODE ] [CODE lang = "csharp" title = "Helper"] using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.IO; WindowsFormsApp1 namespace {Class Aid {public static string file; public static DataTable DataTableFromTextFile (string location, char delimiter = '\ t') {DataTable result; location = file; string [] linearay = File.ReadAllLines (location); result = FromDataTable (linear, delimiter); return result; } Private static DataTable FromDataTable (string [] linearay, char delimiter) {DataTable dt = new DataTable (); AddCoulumnToTable (linear, delimiter, ref dt); AddRowToTable (linear, 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 [] values = .Split values (delimiter); DataRow dr = dt.NewRow (); for (int j = 0; j <values.Length; j ++) {dr [j] = values [j]; } dt.Rows.Add (dr); }} private static void AddCoulumnToTable (string [] columnCollectioi, char delimiter, ref DataTable dt) {string [] colones = columnCollectioi [0] .Split (delimiter); foreach (columnName string in colones) {DataColumn dc = new DataColumn (); dt.Columns.Add (dc); } } } }
 
Please repost your code and format it correctly with line breaks. Also, please post ONLY the relevant code. It's rare that you would need to post the namespace imports so that suggests that you haven't actually trued to determine what code is relevant to the problem. Every bit of irrelevant code you post makes it harder for us to identify the issue and thus makes it less likely that you'll get the help you want.
 
Ok I have two classes namely form1 and Helper I meet you the code the problem is I can only import one file instead of multiple files. Form code:
C#:
 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;
WindowsFormsApp1 namespace {
        public partial class Form1: Form {
                public Form1 () {
                    InitializeComponent ();
                }
            Table DataTable = new DataTable ();
             private void btn_import_Click (object sender, EventArgs e)
             {
                 OpenFileDialog openFileDialog1 = new OpenFileDialog {
                     Title = "Browse file",
                     CheckFileExists = true,
                     CheckPathExists = true ,
                     DefaultExt ", 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 empty label1_Click (object sender, EventArgs e) {
              
            }
        }
}
Helper:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.IO;
WindowsFormsApp1 namespace {
    Class Aid {
        public static string file;
        public static DataTable DataTableFromTextFile (string location, char delimiter = '\ t')
        {
            DataTable result;
            location = file;
            string [] linearay = File.ReadAllLines (location);
            result = FromDataTable (linear, delimiter);
            return result;
        }
        Private static DataTable FromDataTable (string [] linearay, char delimiter)
        {
            DataTable dt = new DataTable ();
            AddCoulumnToTable (linear, delimiter, ref dt);
            AddRowToTable (linear, 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 [] values = .Split values (delimiter);
                DataRow dr = dt.NewRow ();
                for (int j = 0; j <values.Length; j ++)
                {
                    dr [j] = values [j];
                }
                dt.Rows.Add (dr);
            }
        }
        private static void AddCoulumnToTable (string [] columnCollectioi, char delimiter, ref DataTable dt)
        {
            string [] colones = columnCollectioi [0] .Split (delimiter);
            foreach (columnName string in colones)
            {
                DataColumn dc = new DataColumn ();
                dt.Columns.Add (dc);
            }
        }
    }
}
 
Back
Top Bottom