p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   ASP.NET 2.0 Professional (http://p2p.wrox.com/asp-net-2-0-professional-215/)
-   -   Problem in exporting data from datagrid to Excel (http://p2p.wrox.com/asp-net-2-0-professional/60710-problem-exporting-data-datagrid-excel.html)

deb_kareng August 1st, 2007 04:07 AM

Problem in exporting data from datagrid to Excel
 
Hi
   All,

   facing a problem in exporting datagrid to excel.I need some specific column of the grid(not the whole grid)to be exported to excel.have been able to export the whole grid with the following code

Response.Clear();
            Response.Buffer= true;
            Response.ContentType = "application/vnd.ms-excel";
            Response.Charset = "";
            this.EnableViewState = false;
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            this.ClearControls(DataGrid1);
            DataGrid1.RenderControl(oHtmlTextWriter);
            Response.Write(oStringWriter.ToString());
            Response.End();

So, can anybody tell what should i do?


zaghmout August 1st, 2007 06:40 AM

i don't know if that will help, but y don't you bind the column you need to the datagrid and then export it.




Nothing is impossible. The impossible only takes longer. "Digital Fortress, Dan Brown"

planoie August 1st, 2007 07:54 AM

Set all the columns you don't wish to see to Visible=false before you render the control. Then only the column you want to see will show up.

-Peter

deb_kareng August 3rd, 2007 06:30 AM

Thanks
  planoie,

         Your trick works great!!But it only works with the bounded columns only,not been able to work with the autogenerated columns.
Please let me know the solution.


planoie August 4th, 2007 09:21 AM

If you know what columns you don't want to show, then presumably you know what columns you DO want to show, so add the desired columns to the columns collection and turn off autogeneratecolumns.

-Peter


All times are GMT -4. The time now is 04:23 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.