title: Button click pulls wrong DataTable for export into Excel
body: <p>I'm pulling company schedule data from a web API and formatting the data so it can be displayed neatly on a local site and also available to export onto an excel template for distribution across the company.</p>
  <p>I've added a button on the bottom of the site to display the previous weeks schedule and another to export the data to excel. When &quot;Previous Week&quot; is clicked it populates the same Gridview1 table with last weeks data. But When I click the export button it exports the data from the current week into Excel.</p>
  <p>Here's some of my code, let me know what else I need to post for help.</p>
  <p>The datatable gets set on page load into a gridview:</p>
  <pre><code>protected void Page_Load(object sender, EventArgs e)
          {
  
  // ..... bunch of code above to call web API for date range and format table....
  
              DataView view = table.AsDataView();
              view.Sort = &quot;Sort ASC&quot;;
  
              GridView1.DataSource = view;
              GridView1.DataBind();
  
              if (GridView1.Columns.Count &gt; 0)
                  GridView1.Columns[8].Visible = false;
              else
              {
                  GridView1.HeaderRow.Cells[8].Visible = false;
                  foreach (GridViewRow gvr in GridView1.Rows)
                  {
                      gvr.Cells[8].Visible = false;
                  }
              }
  </code></pre>
  <p>and the gridview gets pulled into a datatable and sent to an excel spreadsheet</p>
  <pre><code>protected void ExportExcel(object sender, EventArgs e)
          {
              DataTable dt = new DataTable(&quot;Resident Schedule&quot;);
              foreach (TableCell cell in GridView1.HeaderRow.Cells)
              {
                  dt.Columns.Add(cell.Text);
              }
              foreach (GridViewRow row in GridView1.Rows)
              {
                  dt.Rows.Add();
                  for (int i = 0; i &lt; row.Cells.Count; i++)
                  {
                      dt.Rows[dt.Rows.Count - 1][i] = row.Cells[i].Text;
                  }
              }
              dt.Columns.Remove(&quot;Rotation&quot;);
              dt.Columns.Remove(&quot;Sort&quot;);
  
              using (XLWorkbook wb = new XLWorkbook(@&quot;C:\template.xlsx&quot;))
              {
                  IXLWorksheet ws = wb.Worksheet(&quot;Resident Schedule&quot;);
                  var rangeWithStrings = ws.Cell(5, 2).InsertTable(dt.AsEnumerable(), false);
  
  // ..... bunch of code below
  </code></pre>
  <p>now I have a button click method that gets called to load the previous week into GridView:</p>
  <pre><code>protected void PreviousWeek(object sender, EventArgs e)
          {
  
  // ..... bunch of code above to pull and sort last weeks schedule....
  
              DataView view = table.AsDataView();
              view.Sort = &quot;Sort ASC&quot;;
  
              GridView1.DataSource = view;
              GridView1.DataBind();
  
              if (GridView1.Columns.Count &gt; 0)
                  GridView1.Columns[8].Visible = false;
              else
              {
                  GridView1.HeaderRow.Cells[8].Visible = false;
                  foreach (GridViewRow gvr in GridView1.Rows)
                  {
                      gvr.Cells[8].Visible = false;
                  }
              }
  </code></pre>
  <p>So, my question is, if the button click is repopulating GridView1 data, why is the ExportExcel method is going back to the Page Load data?</p>