I have a streamreader and a streamwriter i've found from another solution here but I keep getting index out of bounds when I try to read what the streamwriter have written to my .txt file (from a datagridview)
Streamwriter:
StreamWriter sw = new StreamWriter("D:\\test.txt");
string line;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
line = "";
foreach(DataGridViewCell cell in row.Cells)
{
line += cell.Value.ToString();
}
sw.WriteLine(line);
}
sw.Close();
Streamreader:
StreamReader sr = new StreamReader("D:\\test.txt");
string line;
string[] values;
int p = 0;
while (!sr.EndOfStream){
line = sr.ReadLine();
values = line.Split(',');
for (int o = 0; o < values.Length; o++){
dataGridView1[p, o].Value = values[o];
}
p++;
}
sr.Close();
my .txt file output (where the each number is written and placed into one cell, 4 cells total):
2345
edit (this works better for me):
StreamReader sr = new StreamReader("D:\\test.txt");
while (!sr.EndOfStream)
{
string[] rows = new string[3];
rows = sr.ReadLine().Split(',');
dataGridView1.Rows.Add(rows[0], rows[1], rows[2], rows[3]);
}
sr.Close();