Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i have a gridview in my asp.net web application. i want to export records on this gridview to excel. It works perfectly when i export all the records to excel. the issue i'm having now is that, i have a dropdown list(to search from names inside the gridview ), textbox and a submit button just about the gridview. I populated the gridview based on the result returned from the database. i also want to export this records to excel. each time i click do the search and click my "export to excel" button, all the records saved into my db are exported. i only want the records returned from my search button to be exported also

What I have tried:

here is my code for exporting to excel. Like i said, it words perfectly when i want to export all the records on the gridview to excel
private void ExportGridToExcel()
{

Response.Clear();

Response.ClearContent();
Response.ClearHeaders();

string FileName = "Example" + DateTime.Now + ".xls";
StringWriter writter = new StringWriter();
HtmlTextWriter html = new HtmlTextWriter(writter);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment,filename" + FileName);
GridView1.HeaderStyle.Font.Bold = true;
GridView1.AllowPaging = false;

search.BindData();
GridView1.DataSource = search.table;
GridView1.DataBind();
GridView1.RenderControl(html);
Response.Write(writter.ToString());
Response.End();
}
protected void Bexcel_Click(object sender, EventArgs e)
{
ExportGridToExcel();
}
How do i modify this to also export records returned from my search query
Posted
Updated 7-Oct-16 9:36am
Comments
Vincent Maverick Durano 7-Oct-16 13:27pm    
You just need to change the datasource of your GridView with the search result set.

1 solution

Get your searched datatable and use it for exporting

C#
protected void Bexcel_Click(object sender, EventArgs e)
{
    DataTable table = GetSearchedData();
    ExportGridToExcel(table);
}


C#
private void ExportGridToExcel(DataTable table)
{

    Response.Clear();
    Response.ClearContent();
    Response.ClearHeaders();

    string FileName = "Example" + DateTime.Now + ".xls";
    StringWriter writter = new StringWriter();
    HtmlTextWriter html = new HtmlTextWriter(writter);
    Response.ContentType = "application/vnd.ms-excel";
    Response.AddHeader("Content-Disposition", "attachment,filename" + FileName);
    GridView1.HeaderStyle.Font.Bold = true;
    GridView1.AllowPaging = false;

    GridView1.DataSource = table;
    GridView1.DataBind();
    GridView1.RenderControl(html);
    Response.Write(writter.ToString());
    Response.End();
}
 
Share this answer
 
Comments
Mcbaloo 10-Oct-16 4:46am    
Thanks for the help. i edited your solution to carry out what i needed. The solution you gave was perfect. Code now working perfectly. thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900