473,382 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

how can I export ALL datagrid rows to excel?

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").GetValu e(control, Nothing))
control.Parent.Controls.Remove(control)
End If
End If
End If
End Sub
</snip>

Nov 19 '05 #1
5 2316
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 )


"The Colonel" <co**********@budweiser.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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").GetValu e(control, Nothing))
control.Parent.Controls.Remove(control)
End If
End If
End If
End Sub
</snip>

Nov 19 '05 #2
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
"The Colonel" <co**********@budweiser.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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").GetValu e(control, Nothing))
control.Parent.Controls.Remove(control)
End If
End If
End If
End Sub
</snip>

Nov 19 '05 #3
Thanks Mr N. - what would that look like?

Nov 19 '05 #4
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.

Nov 19 '05 #5
Got it - I was turning off AllowPaging, but I needed to rebind after
that!

Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Maria L. | last post by:
Hi, I need to export the content of a DataGrid (in Windows application in C#), into an Excel spreadsheet. Anyone knows how to do this? Any code snippets would help! thanks a lot, Maria
0
by: sunilkumar Reddy via DotNetMonster.com | last post by:
hai all first we can take binded grid Export to excel data from ultrawebgrid (or) datagrid rows, Each row data Expoting to one worksheet in excel workbook, like as grid row1 data contain sheet1...
1
by: Diego | last post by:
Hi, I have a ASP.NET application and i'm interested in reporting in excel format. I read a good artile written by Steve C. Orr that gave me info about exporting to excel. ...
8
by: DC Gringo | last post by:
I have a simple button that should open another window and export a datagrid to an Excel file. I'm getting: "Name 'window' is not declared." What do I need to declare or import? <INPUT...
3
by: mattdaddym | last post by:
Hi all, I have been using the following code to export my datagrids to excel for quite some time. Very simple and very effective. <code> Sub btnExcelExport_Click ( s As Object, e As...
0
by: sunilkumar Reddy via DotNetMonster.com | last post by:
hai all first we have taken binded grid Export to excel data from ultrawebgrid (or) datagrid rows, Each row data Expoting to one worksheet in excel workbook, like as grid row1 data contain...
0
by: kieran | last post by:
Hi, I have an 'export to excel' button on my Datagrid. All works well except that on my Datagrid, I have sub total rows. The subtotal rows use colspan so that the subtotals are given above...
4
by: Amirallia | last post by:
Hello Here's my code to export a datagrid to Exel : Response.Clear() Response.Buffer = True Response.ContentType = "application/vnd.ms-excel" Response.Charset = "" Dim monStringWriter As...
2
by: leena13 | last post by:
Hi, The number of rows in the datagrid on my page are approx. around 14130. When I click on the button that exports the records to excel sheet, the page goes blank (I observed that none of the...
2
hemantbasva
by: hemantbasva | last post by:
Note We need to have a template on server for generating report in multiple sheet as we do not had msoffice on server moreover this require a batch job to delete excel file created by the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.