I have stored values retrieved from a text file into an array. Now am trying to store those array values into a data table with the below code.
The problem is that if the first row has 20 values and second row has 23 values, then 43 cells are created in data table of which last 20 (1st row column count) are empty. This continues for each row. Is there any other way to store the array into a data table?
var reader = new StreamReader(File.OpenRead(@"d:\er.txt"));
var table = new DataTable("SampleTable");
while (!reader.EndOfStream)
{
var line = reader.ReadLine().Trim();
var values = line.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
string[] ee = values;
var newRow = table.NewRow();
for (int i = 0; i < ee.Length; i++)
{
table.Columns.Add();
newRow[i] = ee[i].Trim().ToString(); // like sample [0,0]
}
table.Rows.Add(newRow);
}