Click here to Skip to main content
12,444,237 members (45,261 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB ASP.NET VB.NET
Hello Codeproject brothers and sisters

I have this issue that the client wants to export 10,000 rows of data from gridview to excel. I know this is crazy but it is the requirement.

I managed to display the 10,000 rows of data in gridview without having memory exception but the problem is when i exported it to the excel.

I have this code but it is throwing memory exception on this line

datagridview1.RenderControl(hw) 

I tried to do a workaround.This workaround works fine on my local but not on live machine.

_datagridview1.RenderBeginTag(hw)
_datagridview1.HeaderRow.RenderControl(hw)
For Each row As GridViewRow In _datagridview1.Rows
    row.RenderControl(hw)
Next
_datagridview1.FooterRow.RenderControl(hw)
_datagridview1.RenderEndTag(hw)

Still it is throwing memory exception.

Is there anything that i can do in the application pool?
Or is there a better way to do this?

I recommend 3000 rows of data but client doesn't want.

Thanks Guys
Mico
Posted 7-Nov-12 23:25pm
Comments
Mico Perez 8-Nov-12 19:48pm
   
Hi -Dr_X

Have you tested running this code and exporting gridview with 10,000 records?

THanks

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

#Region "GridView Export"
 
		Public Sub ExportGridView(ByVal gridview As GridView)
			PrepareGridViewForExport(gridview)
			RenderGridView(gridview)
		End Sub
 
		Private Sub RenderGridView(ByVal gridview As GridView)
			Dim allowSorting As Boolean = gridview.AllowSorting
			gridview.AllowSorting = False
			Dim attachment As String = "attachment; filename=Export.xls"
			Page.Response.ClearContent()
			Page.Response.AddHeader("content-disposition", attachment)
			Page.Response.ContentType = "application/ms-excel"
			Dim sw As IO.StringWriter = New IO.StringWriter
			Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
			'Create a form to contain the grid
			Dim frm As HtmlForm = New HtmlForm()
			gridview.Parent.Controls.Add(frm)
			frm.Attributes("runat") = "server"
			frm.Controls.Add(gridview)
			frm.RenderControl(htw)
			'gridview.RenderControl(htw)
			Page.Response.Write(sw.ToString)
			Page.Response.End()
			gridview.AllowSorting = allowSorting
		End Sub
 
		Private Sub PrepareGridViewForExport(ByVal gv As Control)
			Dim lb As LinkButton = New LinkButton
			Dim l As Literal = New Literal
			Dim name As String = String.Empty
			Dim i As Integer = 0
			Do While (i < gv.Controls.Count)
				If (gv.Controls(i).GetType Is GetType(LinkButton)) Or (gv.Controls(i).GetType Is GetType(HyperLink)) Or (gv.Controls(i).GetType Is GetType(HyperLinkField)) Then
					l.Text = CType(gv.Controls(i), LinkButton).Text
					gv.Controls.Remove(gv.Controls(i))
					gv.Controls.AddAt(i, l)
				ElseIf (gv.Controls(i).GetType Is GetType(DropDownList)) Then
					l.Text = CType(gv.Controls(i), DropDownList).SelectedItem.Text
					gv.Controls.Remove(gv.Controls(i))
					gv.Controls.AddAt(i, l)
				ElseIf (gv.Controls(i).GetType Is GetType(CheckBox)) Then
					l.Text = CType(gv.Controls(i), CheckBox).Checked
					gv.Controls.Remove(gv.Controls(i))
					gv.Controls.AddAt(i, l)
				End If
				If gv.Controls(i).HasControls Then
					PrepareGridViewForExport(gv.Controls(i))
				End If
				i += 1
			Loop
		End Sub
 

#End Region
  Permalink  
Comments
Sanjay K. Gupta 8-Nov-12 8:37am
   
Is this a best way?

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160811.3 | Last Updated 22 May 2015
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100