コード例 #1
0
 //Wanneer er op de 'klaar' knop wordt gedrukt, schrijf naar een text bestand
 private void btnReady_Click(object sender, EventArgs e)
 {
     if (datagrdArt.Rows.Count > 0)
     {
         string         file_path = "";
         SaveFileDialog sfd       = new SaveFileDialog();
         sfd.Filter = "TXT (*.txt)|*.txt";
         bool fileError = false;
         if ((sfd.ShowDialog() == DialogResult.OK) && (sfd.FileName != ""))
         {
             file_path = sfd.FileName;
             if (File.Exists(sfd.FileName))
             {
                 try
                 {
                     File.Delete(sfd.FileName);
                 }
                 catch (IOException ex)
                 {
                     fileError = true;
                     string            msg     = "Het was niet mogelijk om de data weg te schrijven." + ex.Message;
                     string            title   = "Error";
                     MessageBoxButtons buttons = MessageBoxButtons.OK;
                     MessageBox.Show(msg, title, buttons, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                 }
             }
             if (!fileError)
             {
                 try
                 {
                     StreamWriter sw = new StreamWriter(File.Create(file_path));
                     foreach (DataGridViewRow row in datagrdArt.Rows)
                     {
                         sw.WriteLine(row.Cells[0].Value.ToString());
                         sw.WriteLine(row.Cells[1].Value.ToString());
                         sw.WriteLine(Convert.ToSingle(row.Cells[2].Value));
                     }
                     sw.Close();
                     string            msg     = "Data opgeslagen";
                     string            title   = "Succes";
                     MessageBoxButtons buttons = MessageBoxButtons.OK;
                     DialogResult      result  = MessageBox.Show(msg, title, buttons, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                     SaveArticleList();
                     if (result == DialogResult.OK)
                     {
                         Factuur frm2 = new Factuur();
                         frm2.Show();
                         this.Visible = false;
                     }
                 }
                 catch (Exception ex)
                 {
                     string            msg     = "Error :" + ex.Message;
                     string            title   = "Error";
                     MessageBoxButtons buttons = MessageBoxButtons.OK;
                     MessageBox.Show(msg, title, buttons, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                 }
             }
         }
     }
     else
     {
         string            msg     = "Geen data om te exporteren!";
         string            title   = "Error";
         MessageBoxButtons buttons = MessageBoxButtons.OK;
         MessageBox.Show(msg, title, buttons, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
     }
 }