In Focus
C# Corner Mumbai Chapter: Learn Windows Presentation Foundation (WPF) with CMUG
Jabalpur Chapter Meet July 13: Register Now
Chandigarh Chapter: Developers Day on 29 June 2013
Delhi Chapter Hands On June 22: Limited Seats Register Now
Email :
Password :
Remember me?
Forgot password
Technologies
Monthly Leaders
ASK A QUESTION
C# Programming
Multi-threading
Microsoft Surface
Community Services
Java
.NET General
Office Interoperability
ReFS
Current Affairs
JQuery
Active Directory
Printing
Silverlight 5
Fun and Jokes
JSP
ADO.NET & Database
Project Management
WCF
Job Opportunities
Multimedia, Graphics, Flash
AJAX & Atlas
Remoting
Windows 8
Leadership
PHP
Algorithms & AI
Reporting
Windows Azure
Mac for Windows
Social Networking
Arrays & Collections
Robotics and Hardware
Windows Store Apps
Microsoft Feedback
TypeScript
ASP.NET & Web Development
Security & Cryptography
Workflow Foundation
Open Source Projects
Web Hosting
C# Language
Setup & Deployment
WPF
Operating Systems
Website Management
C# References
Sharepoint
XAML Language
Paid Projects
Windows Azure
CLR & .NET Internals
Speech & Voice Recognition
Site and Forums Feedback
Prizes, Awards, MVP
Windows Phone 7
COM Interoperability
Tablet PC
Announcements
Students & Beginner Projects
Database
Custom Controls
Testing and QA
Author Guidelines
Test Category
Database General
Design and Architecture
Visual Basic .NET
Bugs and Problems
Training & Certification
Oracle
Embedded Development
Visual C++
Forums Feedback
Web Development
SQL
Enterprise Development
Visual Studio 11
Site Feedback & Suggestions
Advertising, Marketing, SEO
SQL Server
Games, DirectX, and XNA
Visual Studio 2010
Site Spams
Android
Products
GDI+ and DirectX
Web Services
Miscellaneous
Cloud Computing
Office 2013
General
Windows Forms
.NET Books
Expression Tools
Products
LINQ
Windows Services
Ask the Author
HTML 5
SharePoint 2013
Migrating to .NET
Cutting-Edge
Career Advice
HTML, JavaScript, CSS
Windows 8
Mobile Development
.NET 5.0
Coffee, Chai Lounge
iPhone, iPad
Forum guidelines
Home
»
ASP.NET & Web Development
»
Run Time Added Columns are not exporting into Excel from DataGrid
Author
Question
Rajesh M
Run Time Added Columns are not exporting into Excel from DataGrid
Posted on: 17 Dec 2012
Hi,
I am new to this Site as well as new in ASP.NET.
I just started working on existing ASP.NET site and need to make some changes on existing website as per my new requirement.
Here, I wanted to export the output data from datagrid to excel, But the issue is runtime adding columns(Selected columns from listbox) are not exporting. For example, I have included 5 standered columns in datagrid and remaining columns will be selected by the user during the runtime based on their requirement. If user selects 3 column in listbox then totally 7 columns are displaying correct but when I export the data into excel only the 5 columns are exporting and the runtime selected columns are not exporting...I tried to sort it out but I could not
Here is my code.
==============
Protected
Sub
Page_Load(
ByVal
sender
As
Object
,
ByVal
e
As
System.EventArgs)
Handles
Me
.Load
Dim
i
As
Integer
Dim
PRIN_ENG_ID
As
String
If
Not
IsPostBack
Then
For
i = 0
To
DataGrid1.Items.Count - 1
If
Len(DataGrid1.Items(i).Cells(4).Text) >= 1
Then
PRIN_ENG_ID = Trim(DataGrid1.Items(i).Cells(4).Text)
If
Len(Trim(PRIN_ENG_ID)) >= 2
Then
Session(
"PRIN_ENG_ID"
) = Trim(PRIN_ENG_ID)
End
If
End
If
Next
CAPS_DB.SelectCommand =
"Select CSS_REPORT_No,IN_QUEUE, UPLOAD_FAILURE,PRIN_ENG_ID,ASSIGNED_COUNT,RESPONDED_DATETIME FROM CAPS_RECORDS WHERE (ACTIVE = 'Y') AND (LEAD_ASSIGNED_REPORT = 'LEAD')"
End
If
End
Sub
Public
Sub
Button1_Click(
ByVal
sender
As
Object
,
ByVal
e
As
System.EventArgs)
Handles
Submit.Click
Session(
"START_DATE"
) = Trim(DateList1.SelectedItem.Text) Session(
"END_DATE"
) = Trim(DateList2.SelectedItem.Text)
START_DATE = (Right(Session(
"START_DATE"
), 2) + Mid(Session(
"START_DATE"
), 4, 2) + Left(Session(
"START_DATE"
), 2)).ToString END_DATE = (Right(Session(
"END_DATE"
), 2) + Mid(Session(
"END_DATE"
), 4, 2) + Left(Session(
"END_DATE"
), 2)).ToString
If
Not
String
.IsNullOrEmpty(START_DATE)
Then
If
SelectList.Items.Count > 0
Then
For
X
As
Integer
= 0
To
SelectList.Items.Count - 1
If
SelectList.Items(X).Selected
Then
sr +=
","
+ SelectList.Items(X).Text
Dim
column
As
New
BoundColumn()
column.HeaderText = SelectList.Items(X).Text
column.DataField = SelectList.Items(X).Text
DataGrid1.Columns.Add(column)
End
If
Next
If
END_DATE > START_DATE
Then
Response.Write(
"<script>alert('Start date should be Higher than End Date');</script>"
)
End
If
If
END_DATE = START_DATE
Then
CAPS_DB.SelectCommand =
"Select CSS_REPORT_No,IN_QUEUE, UPLOAD_FAILURE,PRIN_ENG_ID,ASSIGNED_COUNT,RESPONDED_DATETIME"
& sr &
" FROM CAPS_RECORDS WHERE (ACTIVE = 'Y') AND (LEAD_ASSIGNED_REPORT = 'LEAD') AND (DATE_STRING ='"
& START_DATE &
"')"
' ORDER BY RESPONDED_DATETIME "
End
If
If
END_DATE < START_DATE
Then
CAPS_DB.SelectCommand =
"Select CSS_REPORT_No,IN_QUEUE, UPLOAD_FAILURE,PRIN_ENG_ID,ASSIGNED_COUNT,RESPONDED_DATETIME"
& sr &
" FROM CAPS_RECORDS WHERE (ACTIVE = 'Y') AND (LEAD_ASSIGNED_REPORT = 'LEAD') AND (DATE_STRING BETWEEN '"
& END_DATE &
"' AND '"
& START_DATE &
"')"
'ORDER BY RESPONDED_DATETIME "
End
If
End
If
End
If
End
Sub
Protected
Sub
Export_Click(
ByVal
sender
As
Object
,
ByVal
e
As
System.EventArgs)
Handles
Export.Click
Response.Clear()
Response.Buffer =
True
Response.ContentType =
"application/vnd.ms-excel"
Response.Charset =
""
Me
.EnableViewState =
False
Dim
stringWriter
As
New
System.IO.StringWriter()
Dim
htmlWriter
As
New
System.Web.UI.HtmlTextWriter(stringWriter)
Me
.ClearControls(DataGrid1)
Dim
dg
As
New
DataGrid()
dg.DataBind()
DataGrid1.RenderControl(htmlWriter)
Response.Write(stringWriter.ToString())
Response.End()
End
Sub
Private
Sub
ClearControls(
ByVal
ctrl
As
Control)
Dim
i
As
Integer
For
i = ctrl.Controls.Count - 1
To
0
Step
i - 1
ClearControls(ctrl.Controls(i))
Next
Dim
ctrlType
As
Type = ctrl.GetType()
If
Not
ctrlType.Name =
"TableCell"
Then
If
Not
ctrl.GetType().GetProperty(
"SelectedItem"
)
Is
Nothing
Then
Dim
literal
As
LiteralControl =
New
LiteralControl
ctrl.Parent.Controls.Add(literal)
Try
literal.Text =
CType
(ctrl.GetType().GetProperty(
"SelectedItem"
).GetValue(ctrl,
Nothing
),
String
)
Catch
End
Try
ctrl.Parent.Controls.Remove(ctrl)
ElseIf
Not
ctrl.GetType().GetProperty(
"Text"
)
Is
Nothing
Then
Dim
literal
As
LiteralControl =
New
LiteralControl
ctrl.Parent.Controls.Add(literal)
literal.Text =
CType
(ctrl.GetType().GetProperty(
"Text"
).GetValue(ctrl,
Nothing
),
String
)
ctrl.Parent.Controls.Remove(ctrl)
End
If
End
If
End
Sub
Earlier help would be very much appreciated!!!
Thanks
Rajesh
Smiles :)
Rajesh
Reply
Quick Reply
Report a Spam
Our Recommended Solutions
Export ASP.NET DataGrid to Excel
Adding a ComboBox Column Style to a DataGrid in Windows Forms
How to Add a Counter Column to the DataGrid?
Export Image to Microsoft Office Excel using C#
Export Data Grid in Excel Word And Text File
Silverlight 4 - Export to Excel
Export to Excel functionality using NPOI dll
Revise-Generating ComboBox in a DataGrid Column
Radio Button Column in Datagrid in Silverlight 3
Save Spreadsheet Files to PDF or XPS in Excel 2013
[ + ]
Quick Reply:
SEARCH ANSWERS
All
C# Programming
Cutting-Edge
Site and Forums Feedback
Miscellaneous
Web Development
Database
Products
Any Word
Exact Word
Our recommended forum posts
creating web page for login and edit
How to pass two parameter to reportviewer in C# usingwinform
How to draw with a graphic tablet in VS 2012, .NET 4.5
Export DataGridView content to Excel
Multiple ASP.NET Membership roles in the same Website
post back problem
error:package load failure
Send email by default login details and invisible to sender
Help with C#
Best way to perform Notes to Outlook conversion
HOT QUESTIONS
c# writing user input from form to text file using windows a
C# Exception Handling
System.Collection.IList
code is not working
How do i enter a single value from textbox in sqlserver?
Font Size via C#
How can host WCF Service Application on IIS?
When host to IIS,the styles in CSS can't be applied
C sharp collections....
New User Needs Help with Console Application
SPONSORED BY
Get the industry leading .NET Diagramming component
.NET Diagramming framework designed for creating advanced solutions