Link to home
Create AccountLog in
Visual Basic Classic

Visual Basic Classic

--

Questions

--

Followers

Top Experts

Avatar of G_API
G_API

Read txt file to DataGridView in VB.NET
Dear Experts,

I would like to read a txt file (test.txt) and load the data line by line to DataGridView in vb.net, The data in text file is something like this:
1,yes,100
2,no,50
3,no,60

So, the file needs to be read and split by (,) and then insert these lines to a DataGridView  that has three columns which are
number
agree
salary

The question is that, do you know how can I read the above data, split them and insert them to the DataGridView  in vb.net ?
Waiting for experts, and thanks in advance.
Regards
G_API

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Craig WagnerCraig Wagner🇺🇸


string line;

using( StreamReader reader = new StreamReader( "TextFile1.csv" ) )
{
    while( ( line = reader.ReadLine() ) != null )
    {
        string[] parts = line.Split( ',' );
        dataGridView1.Rows.Add( parts );
    }
}

Open in new window


Avatar of G_APIG_API

ASKER

Hi, thanks for your reply,

How can I use it in VB.net as I have paste the code and gave me errors !

ASKER CERTIFIED SOLUTION
Avatar of Craig WagnerCraig Wagner🇺🇸

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of G_APIG_API

ASKER

Hi CraigWagner,
Its work correctly thanks. About understand and not copy and paste the code, off course, I need a working code so I can understand it line by line and customize as my needs.
Note: I am not a student and this is not a coursework :)
Regards

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.

Visual Basic Classic

Visual Basic Classic

--

Questions

--

Followers

Top Experts

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.