[EDIT #1]
OK, i understand now. You need to add 2 columns for each file:
file1Col1 | file1Col2 | file2Col1 | file2Col2 | file3Col1 | file3Col2 ... fileNCol1 | fileNCol2
and load data into these columns.
Steps to do (logic):
1) create datatable object
2) in a loop -> add columns
3) in a loop -> open file
4) loop through the collection of lines
a) if first file then add rows and add values to the corresponding column (1, 2)
b) if next file then add values to the corresponding column (3,4... n) till line no. < datatable.rows.count else add new row and add values to the corresponding column
5) close file
6) go to the step 3 (open next file)
[/EDIT]
[EDIT #2]
I've made an example for you. I've tested it on 3 files with the structure like this:
line1val1,line1val2,file1line1val3,file1line1val4
line2val1,line2val2,file1line2val3,file1line2val4
line3val1,line3val2,file1line3val3,file1line3val4
line4val1,line4val2,file1line4val3,file1line4val4
line5val1,line5val2,file1line5val3,file1line5val4
line6val1,line6val2,file1line6val3,file1line6val4
In the second and the third file the structure differs as follow:
//3.
line1val1,line1val2,file2line1val3,file2line1val4
...
//4.
...
line6val1,line6val2,file3line6val3,file3line6val4
Here is my code (not well optimized):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.dataGridView1.DataSource = LoadData();
}
private DataTable LoadData()
{
int k = 0;
string[] files = { "E:\\4columns1.txt", "E:\\4columns2.txt", "E:\\4columns3.txt" };
DataTable dt = new DataTable();
DataRow dr = null;
for (int i = 0; i <= files.GetUpperBound(0); i++)
{
string file = files[i];
using (System.IO.StreamReader sr = new System.IO.StreamReader(file))
{
string line = String.Empty ;
int lineno = 0;
while ((line = sr.ReadLine()) != null)
{
string[] split = line.Split(',');
int result = split.Length;
if (lineno == 0)
{
k += 1;
DataColumn dc = new DataColumn("File" + (i+1).ToString() + "Col" + k.ToString(), Type.GetType("System.String"));
dt.Columns.Add(dc);
k += 1;
dc = new DataColumn("File" + (i+1).ToString() + "Col" + k.ToString(), Type.GetType("System.String"));
dt.Columns.Add(dc);
if (dt.Rows.Count <= lineno)
{
dr = dt.NewRow();
dt.Rows.Add(dr);
}
dr = dt.Rows[lineno];
dr[k-2] = split[2];
dr[k-1] = split[3];
}
else
{
if (dt.Rows.Count <= lineno)
{
dr = dt.NewRow();
dt.Rows.Add(dr);
}
dr = dt.Rows[lineno];
dr[k-2] = split[2];
dr[k-1] = split[3];
}
lineno += 1;
}
}
}
return dt;
}
}
}
Test it and change it to your needs.
Note:
You need to increase
lineno
variable inside
if(line.Contains("DISKXFER")) block code.
[/EDIT]