title: Export nested Gridview to Excel error
body: <p>I need to export nested Gridview to Excel with the following code that I get from search, but when I click the Export button it raised an error:</p>
  
  <blockquote>
    <p>An exception of type 'System.ArgumentOutOfRangeException' occurred in
    mscorlib.dll but was not handled in user code</p>
  </blockquote>
  
  <p>This is the code:</p>
  
  <pre><code>protected void ExportExcel(object sender, EventArgs e)
          {
              DataTable dt = new DataTable("GridView_Data");
              GridView grvPayrollDetails = (GridView)grvPayroll.Rows[1].FindControl("grvPayrollDetails");
              foreach (TableCell cell in grvPayroll.HeaderRow.Cells)
              {
                  dt.Columns.Add(cell.Text);
              }
              foreach (TableCell cell in grvPayrollDetails.HeaderRow.Cells)
              {
                  dt.Columns.Add(cell.Text);
              }
              dt.Columns.RemoveAt(0);
              foreach (GridViewRow row in grvPayroll.Rows)
              {
                  GridView grvPayrollDetailscell = (row.FindControl("grvPayrollDetails") as GridView);
                  for (int j = 0; j &lt; grvPayrollDetailscell.Rows.Count; j++)
                  {
                      dt.Rows.Add(row.Cells[1].Text, row.Cells[2].Text, grvPayrollDetailscell.Rows[j].Cells[0].Text, grvPayrollDetailscell.Rows[j].Cells[1].Text);
                  }
              }
              using (XLWorkbook wb = new XLWorkbook())
              {
                  wb.Worksheets.Add(dt);
  
                  Response.Clear();
                  Response.Buffer = true;
                  Response.Charset = "";
                  Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                  Response.AddHeader("content-disposition", "attachment;filename=grvPayrollDetails.xlsx");
                  using (MemoryStream MyMemoryStream = new MemoryStream())
                  {
                      wb.SaveAs(MyMemoryStream);
                      MyMemoryStream.WriteTo(Response.OutputStream);
                      Response.Flush();
                      Response.End();
                  }
              }
          }
  </code></pre>
  
  <p>grvPayroll is the Master Gridview and Gridview grvPayrollDetails is the nested child Gridview. Please help!
  I followed this guide: <a href="http://www.aspforums.net/Threads/147544/Export-Nested-GridView-GridView-inside-GridView-to-Excel-in-ASPNet/" rel="nofollow">Export Nested GridView (GridView inside GridView) to Excel in ASP.Net</a></p>
  
  <p>And this is the Nested Gridview :
  <a href="http://i.stack.imgur.com/1sVFU.png" rel="nofollow">Nested Gridview</a></p>