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. ............................................................................