Excel export from Datagrid

Results 1 to 2 of 2

Thread: Excel export from Datagrid

  1. #1
    Join Date
    Dec 1969

    Default Excel export from Datagrid

    Here is my code from inside Page_Load(): <BR><BR>if( Request.QueryString["bExcel"] == "1" ) <BR> { <BR> Response.ContentType = "application/vnd.ms-excel"; <BR> Response.Charset = ""; <BR> EnableViewState = false; <BR> <BR> System.IO.StringWriter tw = new System.IO.StringWriter(); <BR> System.Web.UI.HtmlTextWriter hw= new System.Web.UI.HtmlTextWriter(tw); <BR> <BR> // Get the HTML for the control. <BR> DG.RenderControl(hw); <BR> <BR> // Write the HTML back to the browser. <BR> Response.Write(tw.ToString()); <BR> <BR> Response.Flush(); <BR> Response.Close(); <BR> } <BR><BR>This works fine with one catch... <BR><BR>I have paging and sorting on my grid set to true. I get an error when I try to export to Excel. The error says &#039;DataGridLinkButton&#039; must be placed inside a form tag. When I set the values to false it works fine. I would obviously like to keep the sorting and paging abilities of my page.<BR><BR>Any ideas?

  2. #2
    Join Date
    Dec 1969

    Default RE: Excel export from Datagrid

    Well, I use the same type of code and don&#039;t have any problems. I don&#039;t use paging, but I do use sorting. The only things I see that I do differently is I use VB.NET and when I populate my datagrid, for instance in the page_load event, I save it to a dataview and store it in a session variable. Then, I create a local copy of the datagrid using my stored session variable and render that. For instance:<BR><BR>In Page_Load:<BR><BR>Dim ldtAdaptor As New SqlDataAdapter(lstrSQL, lconnCCMastRO)<BR>Dim ldtData As DataTable = New DataTable("viewleads")<BR>ldtAdaptor.Fill(ldtData) <BR>Dim ldvData As DataView = ldtData.DefaultView<BR>dgDataGrid1.DataSource = ldvData<BR>dgDataGrid1.DataBind()<BR>Session("view leads") = ldvData<BR><BR>Then when I call the export function instead of just using the dg.rendercontrol(hw) line, I have:<BR><BR>Dim ldvExport As DataView = HttpContext.Current.Session("viewleads")<BR>Dim ldgGrid As New DataGrid()<BR>ldgGrid.DataSource = ldvExport<BR>ldgGrid.DataBind()<BR>ldgGrid.RenderC ontrol(lhtwWriter)<BR><BR>Hope that helps,<BR>Sara

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts