I need a way to open a csv file and write the contents of it into a listview component.
I tried but it doesn't work.
Here is my code:
It won't write display the data! please help!Code:private async void savCoord_Click(object sender, EventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.InitialDirectory = Path.GetPathRoot(Environment.SystemDirectory); saveFileDialog.Filter = "CSV|*.csv"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { using (var writer = new StreamWriter(saveFileDialog.FileName)) { foreach (ListViewItem item in PositionsListView.Items) { await writer.WriteLineAsync($"{item.Text},{item.SubItems[1].Text},{item.SubItems[2].Text},{item.SubItems[3].Text}"); } MessageBox.Show("Success!", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } private void openCoord_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.InitialDirectory = Path.GetPathRoot(Environment.SystemDirectory); openFileDialog.Filter = "CSV|*.csv"; try { if (openFileDialog.ShowDialog() == DialogResult.OK) { string sFileName = openFileDialog.FileName; foreach (var line in File.ReadLines(sFileName)) { var fields = line.Split(','); } } } catch (Exception errorMsg) { MessageBox.Show(errorMsg.Message, "Error reading a file", MessageBoxButtons.OK, MessageBoxIcon.Error); } }




Reply With Quote
