title: Gridview: Export to csv issue
body: <p>I have the following scenario. I have two methods of populating a GridView with data. These are as follows:</p>
  
  <pre><code>protected void btSearch_Click(object sender, EventArgs e)
  {
  
      lqPackWeights.WhereParameters.Clear();
      ControlParameter cp = new ControlParameter();
      cp.Type = TypeCode.String;
  
  
      if (radBuyer.Checked)
      {
          cp.ControlID = "ddlProd";
          cp.PropertyName = "SelectedValue";
          lbRadMiss.Text = "";
      }
  
      else if (radProd.Checked)
      {
          cp.ControlID = "tbxProdAC";
          cp.PropertyName = "Text";
          lbRadMiss.Text = "";
      }
  
      else
      {
          cp.ControlID = "lbRadMiss";
          cp.PropertyName = "Text";
          lbRadMiss.Text = "Please check appropriate radio button before you attempt a search";
      }
  
      cp.Name = "IDDesc";
      lqPackWeights.WhereParameters.Add(cp);
      GridView1.DataSourceID = "lqPackWeights";
      GridView1.DataBind();
  
  }
  </code></pre>
  
  <p>And </p>
  
  <pre><code>protected void btnShowPaper_Click(object sender, EventArgs e)
  {
      ORWeightsDataClassesDataContext dbPa = new ORWeightsDataClassesDataContext();
      int max = 0;
      if (int.TryParse(txtbxHowMany.Text, out max))
      {
          var queryPa = dbPa.tblOnlineReportingCOMPLETEWeights
                      .Where(x =&gt; x.MaterialLevel == "Primary" &amp;&amp; x.MaterialText == "Paper" &amp;&amp; x.MemberId == "FM00012")
                      .OrderByDescending(x =&gt; x.ProductPercentage).Take(max);
  
          GridView1.DataSource = queryPa;
          GridView1.DataBind();
  </code></pre>
  
  <p>These both work fine in populating the gridview with their approrpiate data set.</p>
  
  <p>I also have the following code that exports the gridview content to csv:</p>
  
  <pre><code>protected void btnExportCSV_Click(object sender, EventArgs e)
  {
      Response.Clear();
      Response.Buffer = true;
      Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv");
      Response.Charset = "";
      Response.ContentType = "application/text";
  
      GridView1.AllowPaging = false;
      GridView1.DataBind();
  
      StringBuilder sb = new StringBuilder();
      for (int k = 0; k &lt; GridView1.Columns.Count; k++)
      {
          sb.Append(GridView1.Columns[k].HeaderText + ',');
      }
  
      sb.Append("\r\n");
      for (int i = 0; i &lt; GridView1.Rows.Count; i++)
      {
          for (int k = 0; k &lt; GridView1.Columns.Count; k++)
          {
              sb.Append(GridView1.Rows[i].Cells[k].Text + ',');
          }
          sb.Append("\r\n");
      }
      Response.Output.Write(sb.ToString());
      Response.Flush();
      Response.End();
  
  }
  </code></pre>
  
  <p>The export to csv works perfectly well in the first situation (i.e. the if/else if scenario) but when it comes to the second scenario, only the headings are extracted.</p>
  
  <p>What's going on here then?! I have added in a few breaks and it looks like the export to csv code is being executed, however the actual GridView content is not placed onto the same csv file.</p>
  
  <p>Any ideas?</p>