So in my program the user can choose a file with an OpenFileDialog and if he wants to save the file with a SaveFileDialog, the columns and rows of the csv file should change. For this I have already tried this SaveFileDialog:
List<String> liste = new List<string>();
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "CVS (*.cvs)|*.csv|All files (*.*)|*.*";
if (dialog.ShowDialog() == true)
{
int counter = 0;
string line;
// Read the file and display it line by line.
try
{
System.IO.StreamReader file = new System.IO.StreamReader(path);
while ((line = file.ReadLine()) != null)
{
if (counter == 0)
{
line += ",column1,column2";
liste.Add(line + "\n");
}
else
{
line += $",{"fg"},{"gg"}";
liste.Add(line + "\n");
}
counter++;
}
File.WriteAllLines(dialog.FileName, liste);
file.Close();
}
catch
{
MessageBox.Show("Der Gewählte Prozess wird bereits von einem anderen verwendet,\n " +
" bitte versuchen sie es erneut");
}
However, I cannot change any columns or cells in this way, because it always appends the new lines to the end of the lines. So how do I manage that in the code behind I have the Csv file like this:
after the Change to this: