Click here to Skip to main content
16,017,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi to all, i need to help :-)

i have file.txt with content like below :

00001 JhonKey 023301923
00002 Hercules 023039910

i want load this file to datagridview through datatable, and the result in datagrid like below :

COL1 | COL2 | COL3
-----------------------------------
00001 | JhonKey | 023301923
00002 | Hercules | 023039910

i'm try this code :

C#
using (StreamReader SR = new StreamReader(txtFileName)) 
            {
                int row = 0;
                string line;
                while ((line = SR.ReadLine())!= null)
                {
                    string[] Columns = line.Split(',');
                    dataGridView2.Rows.Add();
                    for (int i = 0; i < Columns.Length; i++)
                    {
                        dataGridView2[i, row].Value = Columns[i];
                    }
                    row++;
                }
            }

but the result not as expected, this wrong like below :

COL1 | COL2 | COL3
--------------------------------------------------------
00001 JhonKey 023301923 | |
00002 Hercules 023039910 | |
Posted

1 solution

The error is this row:

C#
string[] Columns = line.Split(',');


Why do you split on comma instead of blank? Your string looks like "00001 JhonKey 023301923" and not "00001,JhonKey,023301923":

C#
string[] Columns = line.Split(' ');
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900