Quantcast
Channel: CSharp Forum Latest Questions
Viewing all articles
Browse latest Browse all 32751

Run Time Added Columns are not exporting into Excel from DataGrid

$
0
0
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 new requirement.

Here, I wanted to export the output data from datagrid to excel, But the issue is runtime adding columns(Selected columns in listbox) are not exportinng.

Here is my code.
==============

ProtectedSub Export_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles Export.Click
Response.Clear()
'MR
Response.Buffer =
True'MR
Response.ContentType =
"application/vnd.ms-excel"
Response.Charset =
""
Me.EnableViewState = False
Dim stringWriter AsNew System.IO.StringWriter()
Dim htmlWriter AsNew System.Web.UI.HtmlTextWriter(stringWriter)
'ClearControls(DataGrid1) 'Disabled
Me.ClearControls(DataGrid1) 'MR
Dim dg AsNew DataGrid()
dg.DataBind()
DataGrid1.RenderControl(htmlWriter)
Response.Write(stringWriter.ToString())
Response.End()
EndSub



PrivateSub ClearControls(ByVal ctrl As Control)
Dim i AsInteger
For i = ctrl.Controls.Count - 1 To 0 Step i - 1
ClearControls(ctrl.Controls(i))
Next
Dim ctrlType As Type = ctrl.GetType()
IfNot ctrlType.Name = "TableCell"Then
IfNot ctrl.GetType().GetProperty("SelectedItem") IsNothingThen
Dim literal As LiteralControl = New LiteralControl
ctrl.Parent.Controls.Add(literal)
Try
literal.Text =
CType(ctrl.GetType().GetProperty("SelectedItem").GetValue(ctrl, Nothing), String)
Catch
EndTry
ctrl.Parent.Controls.Remove(ctrl)
ElseIfNot ctrl.GetType().GetProperty("Text") IsNothingThen
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)
EndIf
EndIf
EndSub

Earlier help would be very much appreciated!!!

Thanks
   Rajesh

Viewing all articles
Browse latest Browse all 32751

Trending Articles