Morning all - another day, another question from Rich!
I have the following scenario. I have two methods of populating a GridView with data. These are as follows:
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();
}
And
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 => x.MaterialLevel == "Primary" && x.MaterialText == "Paper" && x.MemberId == "FM00012")
.OrderByDescending(x => x.ProductPercentage).Take(max);
GridView1.DataSource = queryPa;
GridView1.DataBind();
These both work fine in populating the gridview with their approrpiate data set.
I also have the following code that exports the gridview content to csv:
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 < GridView1.Columns.Count; k++)
{
sb.Append(GridView1.Columns[k].HeaderText + ',');
}
sb.Append("\r\n");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
for (int k = 0; k < 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();
}
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.
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.
Any ideas?