Dear all,
Good day,
I have reader text file but the length of file not static
my code is :
public class jr
{
public string name1 { get; set; }
public string name2 { get; set; }
public string name3 { get; set; }
public string name4 { get; set; }
public string name5 { get; set; }
public string name6 { get; set; }
public string name7 { get; set; }
public string name8 { get; set; }
public string name9 { get; set; }
public static List<jr> LoadUserListFromFile(string path)
{
var users = new List<jr>();
foreach (var line in File.ReadAllLines(path))
{
var delimiters = new char[] { ',' };
var columns = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
users.Add(new jr
{
name1 = columns[0].Trim(),
name2 = columns[1].Trim(),
name3 = columns[2].Trim(),
name4 = columns[3].Trim(),
name5 = columns[4].Trim(),
name6 = columns[5].Trim(),
name7 = columns[6].Trim(),
name8 = columns[7].Trim(),
name9 = columns[8].Trim(),
}
);
}
return users;
}
}
to read it in datagridview :
dataGridView1.DataSource = jr.LoadUserListFromFile("file path");
for some of files i can read but another files i received error index was outside the bounds of the array
so i need to make my code dynamic not static for specific files.
I mean like check the length of file and create columns as the length
I hope every things is clear.
Thank you for your help.