Gridview: Export to csv issue
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?
There is a javascript function you need to add. Check out this blog post which explains it:
http://www.inspiredbytechnology.com/index.php/2011/03/09/export-gridview-to-csv-in-a-sharepoint-2010-webpart/
Thanks
Jon
Make sure you are rebinding your data BEFORE you do your export. I ran into this same issue a while back on a project, as we had VIEWSTATE turned off on the GridView, so on the postback we were trying to export the data, prior to the grid being populated with data.
I actually resolved this issue by removing the GridView1.AllowPaging = false; GridView1.DataBind(); from the export script.
Not sure why it works...but it does!
精彩评论