0

I am working on a Program which allows to open and save data from/to a file and does the same with a database if you wish. So far I only have some Text columns, saving those was no big deal, now my teacher asked me to add a picture column, which also worked. But as my datagridwiev was only text when I created it, I could easily save it in a text file... with a picture this would no longer work. Using my old code:

The txt file displays something like
--> col1 | col2 | col3 | System.Drawing.Bitmap

any Ideas how I can change my saveFileDialog so it would work with both text and an image?

what I have so far:

 private void button1_Click_1(object sender, EventArgs e) //Save to File  Button
    {

        SaveFileDialog SaveDialog = new SaveFileDialog();

        SaveDialog.InitialDirectory = @"C:\";
        SaveDialog.Title = "Save text Files";
        SaveDialog.CheckFileExists = false;
        SaveDialog.CheckPathExists = false;
        SaveDialog.DefaultExt = "txt";
        SaveDialog.Filter = "txt files (*.txt)|*txt";

        if (SaveDialog.ShowDialog() == DialogResult.OK)
        {

            var csv = new StringBuilder();
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                var NameCell = dataGridView1.Rows[i].Cells["ColName"].Value;
                var AdrCell = dataGridView1.Rows[i].Cells["Adresse"].Value;
                var TelCell = dataGridView1.Rows[i].Cells["Telefon"].Value;
                var PicCell = dataGridView1.Rows[i].Cells["Bild"].Value;

                var newLine = string.Format("{0}|{1}|{2}|{3}", NameCell, AdrCell, TelCell);
               
                csv.AppendLine(newLine);
            }

            File.WriteAllText(SaveDialog.FileName, csv.ToString());
            
            MessageBox.Show("Saved!");
        }
    }
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Eileen
  • 3
  • 2
  • 8
  • A text file can't contain an image, you'll need to save it to a different type of file. – gunnerone Jun 06 '18 at 15:12
  • You can Use DrawToBitmap to get an image of the DGV. [See here](https://stackoverflow.com/questions/35544789/drawtobitmap-returning-blank-image/35571419?s=1|68.3262#35571419) - If you actually want to save text you can save the filepaths if you have them. Or, of course you can save to bitmaps to a blob column, depending on the dbms.. – TaW Jun 06 '18 at 15:28
  • so the only way would be to save the text and images in two different files? – Eileen Jun 07 '18 at 07:03
  • Export to Excel Copy/Paste? https://stackoverflow.com/a/39314154/495455 – Jeremy Thompson Jan 11 '23 at 01:59

0 Answers0