Suraj Dhungana
Suraj Dhungana

Reputation: 1

Read text from txt file and split it

How to read and split text in separated column? In a file It contains this text given below.

[HRData]
97  0   0   99  0   50 
97  0   0   99  0   50
97  0   0   99  0   50

code

bool HRData = false;
using (StreamReader sr = new StreamReader(path))
{
    string line = string.Empty;
    while ((line = sr.ReadLine()) != null)
    {
        if (!HRData)     
        {
            if (line == "[HRData]")
            {
                HRData = true;
                continue;
            } 

            string[] columns = line.Split('\t');
            dataGridView2.Rows.Add(columns);
        }
    }
}

Output

Split string should be in separate column in DataGridView

|97|0|0|99|50|
|97|0|0|99|50|
|97|0|0|99|50|

Upvotes: 0

Views: 413

Answers (2)

SanjayK
SanjayK

Reputation: 61

  • Extending original answer- To get exact split charater for your file you will have to share sample text file data so we can suggest the split logic

  • As per current logic you are setting HRData bool flag true when [HRData] text is found in a line, this will cause next ReadLine() iteration in while loop to not split as you are negating the flag in IF, "if (!HRData)"

  • And your main query as I understood is you need to have splitted data, each word/string in separate column, but you are adding Rows as single column array hence all data is in first column

  • You can refer below and use Datatable to first add columns from header row or by statically hard-coding columns, then add each row using split data then set DataTable as source for GridView

  • Please note I have written code in VS 2017 but haven't run it to test its working

  • Edit- Modified code as per updated sample data in original question

             DataTable dataTable = new DataTable();
             using (StreamReader sr = new StreamReader(path))
             {
                 string line = string.Empty;
                 //Decide how many maximum columns/splits file will have or calculate max columns separately first by looping through file lines
                 int index = -1;
                 dataTable.Columns.Add(new DataColumn("Column1", Type.GetType("String")));
                 dataTable.Columns.Add(new DataColumn("Column2", Type.GetType("String")));
                 dataTable.Columns.Add(new DataColumn("Column3", Type.GetType("String")));
                 dataTable.Columns.Add(new DataColumn("Column4", Type.GetType("String")));
                 dataTable.Columns.Add(new DataColumn("Column5", Type.GetType("String")));
                 dataTable.Columns.Add(new DataColumn("Column6", Type.GetType("String")));
                 while ((line = sr.ReadLine()) != null)
                 {
                     index++;
                     //If header row is present add columns with names
                     //string[] columns = null;
                     //if (index == 0)//Header row to add as columns with names
                     //{
                     //    columns = line.Split(' ');
                     //    foreach (var colName in columns)
                     //    {
                     //        dataTable.Columns.Add(new DataColumn(colName, Type.GetType("String")));
                     //    }
                     //}
                     if (line == "[HRData]" || string.IsNullOrEmpty(line))
                     {
                         continue;
                     }
                     var row = dataTable.NewRow();
                     row.ItemArray = line.Split(' ');
                     dataTable.Rows.Add(row);
                 }
             }
             dataGridView2.DataSource = dataTable;
    

Upvotes: 0

Chris Schaller
Chris Schaller

Reputation: 16554

Your text input is delimited by spaces not tabs, so as pointed out by @bhavya in the comments, you need to change the argument passed through to the Split function to line.Split(" ");:

bool HRData = false;
using (StreamReader sr = new StreamReader(path))
{
    string line = string.Empty;
    while ((line = sr.ReadLine()) != null)
    {
        if (!HRData)
        {
            if (line == "[HRData]")
            {
                HRData = true;
                continue;
            }         
            string[] columns = line.Split(" ");

            dataGridView2.Rows.Add(columns);
        }
    }
}

Upvotes: 1

Related Questions