title: Export Gridview to Excel: Cannot export the correct data displayed
body: <p>I need to export my gridview data to excel. No problems for this.
  All works correctly
  The problem is i can apply filters to the data displayed in the grid. And when i want to export only those data (once the filter is applied), it exports ALL the data contained previously in my grid before the filter.</p>
  
  <p>I hope i'm clear enough...</p>
  
  <p>This is the code i'm using:</p>
  
  <pre><code>    public void ExportGridToExcel(GridView grdGridView, string fileName)
      {
          Response.Clear();
          Response.Buffer = true;
  
          Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
          Response.Charset = "";
          Response.ContentType = "application/vnd.ms-excel";
          StringWriter sw = new StringWriter();
          HtmlTextWriter hw = new HtmlTextWriter(sw);
  
          grdGridView.AllowPaging = false;
          grdGridView.DataBind();
  
          //Change the Header Row back to white color
          grdGridView.HeaderRow.Style.Add("background-color", "#FFFFFF");
  
          // Apply style to Individual Cells
          grdGridView.HeaderRow.Cells[0].Style.Add("background-color", "green");
          grdGridView.HeaderRow.Cells[1].Style.Add("background-color", "green");
          grdGridView.HeaderRow.Cells[2].Style.Add("background-color", "green");
          grdGridView.HeaderRow.Cells[3].Style.Add("background-color", "green");
          grdGridView.HeaderRow.Cells[4].Style.Add("background-color", "green");
          grdGridView.HeaderRow.Cells[5].Style.Add("background-color", "green");
          grdGridView.HeaderRow.Cells[6].Style.Add("background-color", "green");
  
          for (int i = 0; i &lt; grdGridView.Rows.Count; i++)
          {
              GridViewRow row = grdGridView.Rows[i];
  
              // Change Color back to white
              row.BackColor = System.Drawing.Color.White;
              // Apply text style to each Row
              row.Attributes.Add("class", "textmode");
              // Apply style to Individual Cells of Alternating Row
              if (i % 2 != 0)
              {
                  row.Cells[0].Style.Add("background-color", "#C2D69B");
                  row.Cells[1].Style.Add("background-color", "#C2D69B");
                  row.Cells[2].Style.Add("background-color", "#C2D69B");
                  row.Cells[3].Style.Add("background-color", "#C2D69B");
                  row.Cells[4].Style.Add("background-color", "#C2D69B");
                  row.Cells[5].Style.Add("background-color", "#C2D69B");
                  row.Cells[6].Style.Add("background-color", "#C2D69B");
              }
          }
          grdGridView.RenderControl(hw);
  
          // style to format numbers to string
          string style = @"&lt;style&gt; .textmode { mso-number-format:\@; } &lt;/style&gt;";
          Response.Write(style);
          Response.Output.Write(sw.ToString());
          Response.Flush();
          Response.End();
      }
  </code></pre>
  
  <p>What i do to apply filters ( all works perfectly )</p>
  
  <pre><code>    public void refreshGridFactures()
      {
          DateTime? debutFacture = null;
          DateTime? FinFacture = null;
          DateTime? DebutReglement = null;
          DateTime? FinReglement = null;
          int? NumeroFacture = 0;
  
          if (PickerDateDebut.SelectedDate != null) { debutFacture = PickerDateDebut.SelectedDate; }
          if (PickerDateFin.SelectedDate != null) { FinFacture = PickerDateFin.SelectedDate; }
          if (PickerDebutReglement.SelectedDate != null) { DebutReglement = PickerDebutReglement.SelectedDate; }
          if (PickerFinReglement.SelectedDate != null) { FinReglement = PickerFinReglement.SelectedDate; }
          int IdEntreprise = Convert.ToInt32(ddlEntreprises.SelectedValue);
          string Intitule = txtIntitule.Text;
          if (txtNumeroFacture.Text != "") { NumeroFacture = Convert.ToInt32(txtNumeroFacture.Text); }
  
          List&lt;DBAccess.Facturation&gt; listFacturation = DBAccess.DAOFacturation.GetFactures(IdEntreprise, debutFacture, FinFacture, DebutReglement, FinReglement, Intitule, NumeroFacture);
          GridFactures.DataSource = listFacturation;
          GridFactures.DataBind();
  
          double total = Math.Round(listFacturation.Sum(a =&gt; a.MontantTTC), 2);
          double totalHT = Math.Round(listFacturation.Sum(a =&gt; a.MontantHT), 2);
  
          lblTotalTTC.Text = "Montant total TTC : " + total;
          lblTotalHT.Text = "Montant total HT : " + totalHT;
      }
  </code></pre>
  
  <p>Thanks for your help</p>