Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void button1_Click(object sender, EventArgs e)
- {
- saveFileDialog1.ShowDialog();
- string path = saveFileDialog1.FileName;
- label1.Text = saveFileDialog1.FileName;
- if(path != "") //Om vi cancel i savedialog ska vi ej skapa filen
- {
- string csv = "";
- //; i Sverige, olika för olika länder?
- foreach (DataGridViewColumn column in dataGridView1.Columns)
- {
- csv += column.HeaderText + ';';
- }
- //Add new line.
- csv += "\n";
- //Adding the Rows
- foreach (DataGridViewRow row in dataGridView1.Rows)
- {
- foreach (DataGridViewCell cell in row.Cells)
- {
- if(cell.Value == null) // Tar man bort all text från "Comment" returnerar den null istället för ""
- {
- csv += "" + ';';
- }
- else
- {
- csv += cell.Value.ToString() + ';';
- }
- }
- //Add new line.
- csv += "\n";
- }
- if (path.Substring(path.Length - 4) == ".csv") // Så att filnamnet ej blir .csv.csv när man klickar på en gammal fil i SaveDialog
- path = path.Remove(path.Length - 4);
- try
- {
- File.WriteAllText(path + ".csv", csv);
- }
- catch(System.IO.IOException)
- {
- MessageBox.Show("You can't save the file because it is being used by another program");
- }
- saveFileDialog1.FileName = ""; // Reset, annars kommer den spara fastän vi klickar cancel
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement