Hi!
I have a application that I can open a csv file with, then modify it and then export it back to my computer.
it works well and all but there's one problem with it.
When I export the datagridview as csv it creates two plank extra rows on the bottom of the csv file.
I have tried pretty much everything that I can do but it still brings the rows.
My code for the export looks like this:
Any help is appriciated, Thanks.
-Mazon
I have a application that I can open a csv file with, then modify it and then export it back to my computer.
it works well and all but there's one problem with it.
When I export the datagridview as csv it creates two plank extra rows on the bottom of the csv file.
I have tried pretty much everything that I can do but it still brings the rows.
My code for the export looks like this:
C#:
private void btnExport_Click(object sender, EventArgs e)
{
// Don't save if no data is returned
if (dataGridView1.Rows.Count ==0)
{
return;
}
StringBuilder sb = new StringBuilder();
sb.AppendLine(string.Join(",", dataGridView1.Columns.Cast<DataGridViewColumn>().Select(x => $"\"{x.Name}\"")));
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (!row.IsNewRow)
{
sb.AppendLine(string.Join(",", row.Cells.Cast<DataGridViewCell>().Select(c => $"\"{c.Value ?? ""}\"")));
}
}
// Load up the save file dialog with the default option as saving as a .csv file.
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "CSV files (*.csv)|*.csv";
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
// If they've selected a save location...
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(sfd.FileName, false))
{
// Write the stringbuilder text to the the file.
sw.WriteLine(sb.ToString());
}
}
Any help is appriciated, Thanks.
-Mazon
Last edited by a moderator: