title: Save DataGridView changes by closing form
body: <p>I'm writing a program in which i can add items to a <code>DataGridView</code>. This works perfectly. And I got a Delete button, to remove the items from <code>DataGridView</code>. This is working, but not perfectly. It delets my items but if I restart the program, the items are still here. I'm saving the items to an xml file. So here you can see my code for adding items to <code>DataGridView</code>:</p>
  
  <pre><code>private void btnSave_Click(object sender, EventArgs e)
          {
              if (!File.Exists(@"C:\Users\rs\Desktop\Save\save.xml"))
              {
                  using (File.Create(@"C:\Users\rs\Desktop\Save\save.xml")) { }
              }
  
              xmlnode = new XElement("iToDo",
                      new XElement("Name", txtName.Text),
                      new XElement("Priorität", comPrio.Text),
                      new XElement("Anfangsdatum", txtStart.Text),
                      new XElement("Enddatum", txtEnd.Text),
                      new XElement("Bemerkung", txtComment.Text)
              );
  
              try
              {
                  xmlFile = XElement.Load(@"C:\Users\rs\Desktop\Save\save.xml");
                  xmlFile.Add(xmlnode);
              }
              catch (XmlException)
              {
                  xmlFile = new XElement("ToDos", xmlnode);
              }
  
              xmlFile.Save(@"C:\Users\rs\Desktop\Save\save.xml");
              DataSet flatDataSet = new DataSet();
              flatDataSet.ReadXml(@"C:\Users\rs\Desktop\Save\save.xml");
              DataTable table = flatDataSet.Tables[0];
              dataGridToDo.DataSource = table;
          }
  </code></pre>
  
  <p>And here you can see my code for deleting rows:</p>
  
  <pre><code>private void btnDelete_Click(object sender, EventArgs e)
          {
              foreach (DataGridViewRow row in this.dataGridToDo.SelectedRows)
              {
                  dataGridToDo.Rows.RemoveAt(row.Index);
              }
          }
  </code></pre>
  
  <p>And I tried to put this in <code>Form_Closing()</code>, which not works:</p>
  
  <pre><code>private void Form1_FormClosing(object sender, FormClosingEventArgs e)
          {
              e.Cancel = false;
              Application.Exit();
          }
  </code></pre>
  
  <p>Can someone give me a hint how I can fix this?</p>
  
  <p>Thanks in advance</p>
  
  <p>Cheers</p>