Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > BOOK: Beginning ASP.NET Databases
|
BOOK: Beginning ASP.NET Databases Also see the forum ASP Databases for more general discussions of ASP database issues not directly related to these books.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 13th, 2009, 08:46 AM
Registered User
 
Join Date: May 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Exporting a dynamically created GridView to Excel

Greetings everyone!

OK, I see many examples out there on how I can export a gridview to Excel. I can even get them to work! It is pretty straight forward. What I need is to be able to export to Excel a GridView created dynamically from a code behind page using VB. My application has many options to the user and the number of columns varies.

Any help and/or guidance would be greatly appreciated.

Thank you,

Gary

Here is my code on the .aspx page;
<asp:PlaceHolderID="ph1"runat="server"></asp:PlaceHolder>


Here is my code on the code behind page (.aspx.vb);
Code:
 
Dim Cols, x AsIntegerDim ColsData AsStringDim gridview1 AsNew GridView
 
Cols = 0
x = 0
 
Cols = dtQuery.Columns.Count
 
DoUntil x >= Cols
 
ColsData = dtQuery.Columns.Item(x).ColumnName
 
gridview1.AutoGenerateColumns = FalseDim Field AsNew BoundField
 
Field.DataField = ColsData
Field.HeaderText = ColsData
Field.HeaderStyle.BackColor = Drawing.ColorTranslator.FromHtml("#0021a5")
Field.HeaderStyle.ForeColor = Drawing.Color.White
Field.HeaderStyle.Font.Name = "Verdana"  Field.HeaderStyle.Font.Size = FontUnit.Small   Field.ItemStyle.Font.Name = "Verdana"  Field.ItemStyle.Font.Size = FontUnit.Small
 
If x = 0 Then     Field.ItemStyle.BackColor = Drawing.Color.LightGray
Else     Field.ItemStyle.BackColor = Drawing.Color.White
EndIf
Field.ItemStyle.ForeColor = Drawing.Color.Black
Field.ItemStyle.BackColor = Drawing.Color.White
 
 
Dim Col As DataControlField = Field
gridview1.Columns.Add(Col)
 
x = x + 1
Loop
gridview1.DataSource = dtQuery
gridview1.DataBind()
 
ph1.Controls.Add(gridview1)
 
'.... I have tried to bind to an <asp:GridView.... but I cannot get that to export either.......
'.... and the application adds an additional GridView on the aspx page instead of  .........
'.... replacng the one that is there. ............................................................................







Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help me about dynamically created controls vingo_mail ASP.NET 2.0 Basics 1 March 24th, 2008 12:54 PM
Exporting gridview into excel yukijocelyn ASP.NET 2.0 Basics 0 September 27th, 2007 10:38 PM
Dynamically created code Puck312 Pro VB 6 3 September 7th, 2005 01:21 PM
how delete objects created dynamically? renee_santoscoi Javascript 1 October 7th, 2004 08:08 PM
Dynamically created controls katsarosj ASP.NET 1.0 and 1.1 Basics 4 February 4th, 2004 09:13 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.