Reputation: 1242
I've got a function to export some data from the GridView
to Excel, which is bound to some checkboxes. So it only exports the rows that are checked.
This works fine, however I can't seem to disable paging for the export?
This is my code:
private void ExportGridToExcel()
{
bool isSelected = false;
foreach (GridViewRow i in GridView1.Rows)
{
CheckBox cb = (CheckBox)i.FindControl("chkSelect");
if (cb != null && cb.Checked)
{
isSelected = true;
break;
}
}
if (isSelected)
{
GridView gvExport = GridView1;
// this below line for not export checkbox to excel file
gvExport.Columns[0].Visible = false;
foreach (GridViewRow i in GridView1.Rows)
{
gvExport.Rows[i.RowIndex].Visible = false;
CheckBox cb = (CheckBox)i.FindControl("chkSelect");
if (cb != null && cb.Checked)
{
gvExport.Rows[i.RowIndex].Visible = true;
}
}
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=ExportGridData.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htW = new HtmlTextWriter(sw);
Response.ContentEncoding = System.Text.Encoding.Unicode;
Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
Response.Cache.SetCacheability(HttpCacheability.NoCache);
gvExport.AllowPaging = false;
gvExport.RenderControl(htW);
Response.Output.Write(sw.ToString());
Response.End();
}
}
protected void Button3_Click(object sender, EventArgs e)
{
ExportGridToExcel();
}
As you can see I've set AllowPaging = false
but the page numbers are still visible in the exported file. Anybody got any idea why?
Upvotes: 1
Views: 4435
Reputation: 1
The code block below is operational.
GridView2.AllowPaging = false;
GridView2.DataSource = GetProducts(); //Data Source
GridView2.DataBind();
GridView2.RenderControl(htw);
Upvotes: 0
Reputation: 11
Before rendering - disable the paging, bind the data and then render:
gvExport.AllowPaging = false;
gvExport.DataSource = ds; //Data Source
gvExport.DataBind();
gvExport.RenderControl(objHtmlTextWriter)
Upvotes: 0
Reputation: 46005
To export the whole data from your GridView you have to re-bind the Datasource
//1.bind with paging disabled
gvExport.AllowPaging = false;
gvExport.DataBind();
//2.export method here
//3.bind with paging enabled
gvExport.AllowPaging = true;
gvExport.DataBind();
but if it's possible i would export the data directly from the datasource because you have to remove headers, hide columns, replace
and handle controls like asp:Checkbox
seperately
This is a good approach if you want to export from your asop:GridView
- http://forums.asp.net/post/4222334.aspx
Upvotes: 3