title: save data from datagridview to xml
body: <p>I have 1 form, where <em>button1</em> draw table at <em>datagridview</em>.
  After filling the cells, i need to save data from <em>datagridview</em> to xml by <em>button2</em>.</p>
  
  <pre><code>//save
  private void button1_Click(object sender, EventArgs e)
          {        
              try
              {                      
                  DataSet ds = new DataSet();
                  ds = (DataSet)(dataGridView1.DataSource);
                  ds.WriteXml(@"c:\1.xml");
              }
              catch (Exception ex)
              {
                  MessageBox.Show("Ошибка: " + ex.Message);
              }            
          }
  
  //draw a datagridview    
  private void button2_Click(object sender, EventArgs e)
              {
                  try
                  {
                      int x, y;
                      x = Convert.ToInt32(textBox1.Text);
                      y = Convert.ToInt32(textBox2.Text);
                      double[,] grid;
                      grid = new double[x, y];
                      int k = 0;
                      k = dataGridView1.ColumnCount;
                      if (k != 0)
                          for (int i = 0; i &lt; k; i++)
                              dataGridView1.Columns.RemoveAt(0);
                      dataGridView1.Columns.Clear();
                      AddColumns(y, dataGridView1);
                      AddRows(x, dataGridView1);
                      dataGridView1.RowHeadersWidth = 22;                                    
                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show("Ошибка: " + ex.Message);
                  }
  
              }
  </code></pre>
  
  <p>but i received error: </p>
  
  <blockquote>
    <p>object reference not set to an instantce of an object.</p>
  </blockquote>
  
  <p><strong>So, how can i save datagridview to xml?</strong>  </p>