Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

VB
datagridview1.RenderControl(hw) 


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

VB
_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
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

VB
#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
 
Share this answer
 
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)



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