how can I export ALL datagrid rows to excel?

T

The Colonel

I'm only getting current page. I've tried playing with the
PageSize and AllowPaging = False before exporting, but no luck. Here's
my code:

<snip>


Public Sub btnExport_OnClick(ByVal sender As Object, ByVal e As
EventArgs)


Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-Disposition", "inline;filename=" &
gstrTableName & ".xls")
Response.Charset = ""


Me.EnableViewState = False
ClearControls(dgParent)


Dim tw As New System.IO.StringWriter
Dim hw As New System.Web.UI.HtmlTextWriter(tw)


' Get the HTML for the control.
dgParent.PageSize =
gobjDataSet.Tables(gstrTableName).Rows.Count
dgParent.RenderControl(hw)


' Write the HTML back to the browser.
Response.Write(tw.ToString())


' End the response.
Response.End()


End Sub


Private Sub ClearControls(ByVal control As Control)


Dim i As Integer
For i = control.Controls.Count - 1 To 0 Step -1
ClearControls(control.Controls(i))
Next


If Not TypeOf control Is TableCell Then
If Not (control.GetType().GetProperty("SelectedItem") Is
Nothing) Then
Dim literal As New LiteralControl
control.Parent.Controls.Add(literal)
Try
literal.Text =
CStr(control.GetType().GetProperty("SelectedItem").GetValue(control,
Nothing))
Catch
End Try
control.Parent.Controls.Remove(control)
Else
If Not (control.GetType().GetProperty("Text") Is
Nothing) Then
Dim literal As New LiteralControl
control.Parent.Controls.Add(literal)
literal.Text =
CStr(control.GetType().GetProperty("Text").GetValue(control, Nothing))
control.Parent.Controls.Remove(control)
End If
End If
End If


End Sub


</snip>
 
M

Mr Newbie

I guess Im too tired to really give much here, but the the first question is
why you are not using the object to which the DataGrid is bound to export
from, rather than the grid itself ?


Regards - Mr N ( Who is off to sleep now )
 
S

S. Justin Gengo

I have an example of this on my website, www.aboutfortunate.com, (your code
is very close). Click on the "Code Library" link at the top of the page and
then click on the datagrid row "Ouput a datagrid as an excel spreadsheet
page".

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
T

The Colonel

Thanks, Justin. It is very close - I guess the only difference is that
I'm displaying the grid with an export button, so the export is
actually taking place in an OnClick sub. I'll probably have to go a
different route.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top