This is a migrated thread and some comments may be shown as answers.

Column headings not showing on export

13 Answers 792 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Web Services
Top achievements
Rank 2
Web Services asked on 13 Dec 2011, 10:34 PM
I have a rad grid that I'm exporting to excel. The column headers never show in the excel file. Any ideas why? Here's the code I use to export, 
RadGrid1.DataBind()
        RadGrid1.ExportSettings.OpenInNewWindow = True
        RadGrid1.ExportSettings.ExportOnlyData = False
        RadGrid1.ExportSettings.HideStructureColumns = False
        RadGrid1.ExportSettings.IgnorePaging = True
        RadGrid1.MasterTableView.ExportToExcel()

13 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 14 Dec 2011, 04:44 AM
Hello,

Take a look into the following forum thread which discussed the same.
Header is not exporting during export to excel

Thanks,
Princy.
0
Web Services
Top achievements
Rank 2
answered on 14 Dec 2011, 11:16 PM
I'm pretty sure I have the columns added correctly. Here's the full file.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Reporting.aspx.vb" Inherits="Reporting" %>
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%@ Register TagName="link" Src="~/Links.ascx" TagPrefix="control" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <control:link ID="linkControl" runat="server" />
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
        <br />
        <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" Visible="false">
<MasterTableView AutoGenerateColumns="True" DataSourceID="SqlDataSource1">
<RowIndicatorColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
 
<ExpandCollapseColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
    <Columns>
        <telerik:GridBoundColumn DataField="FirstName" HeaderText="First Name"
            SortExpression="FirstName" UniqueName="FirstName">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="LastName" HeaderText="Last Name"
            SortExpression="LastName" UniqueName="LastName">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Address" HeaderText="Address"
            SortExpression="Address" UniqueName="Address">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="City" HeaderText="City"
            SortExpression="City" UniqueName="City">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="State" HeaderText="State"
            SortExpression="State" UniqueName="State">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Zip" HeaderText="Zip" SortExpression="Zip"
            UniqueName="Zip">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Phone" HeaderText="Phone"
            SortExpression="Phone" UniqueName="Phone">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Email" HeaderText="Email"
            SortExpression="Email" UniqueName="Email">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="DateFor" HeaderText="Date For"
            SortExpression="DateFor" UniqueName="DateFor">
        </telerik:GridBoundColumn>
    </Columns>
</MasterTableView>
        </telerik:RadGrid>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:connection %>"
            SelectCommand="SELECT [FirstName], [LastName], [Address], [City], [State], [Zip], [Phone], [Email], [DateFor] FROM [People] ORDER BY LastName">
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>


Imports Telerik.Web.UI
Partial Class Reporting
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        RadGrid1.DataBind()
        RadGrid1.ExportSettings.OpenInNewWindow = True
        RadGrid1.ExportSettings.ExportOnlyData = False
        RadGrid1.ExportSettings.HideStructureColumns = False
        RadGrid1.ExportSettings.IgnorePaging = True
        RadGrid1.MasterTableView.ExportToExcel()
    End Sub
End Class


0
Princy
Top achievements
Rank 2
answered on 15 Dec 2011, 05:23 AM
Hello,

No need of RadGrid1.DataBind() in PageLoad. Also set AutoGenerateColumns as false.

Thanks,
Princy.
0
Web Services
Top achievements
Rank 2
answered on 16 Dec 2011, 04:11 PM
I set auto generate to false. If I remove the data bind statement, then I get this error.




RadGrid must be databound before exporting.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: RadGrid must be databound before exporting.

Source Error: 

Line 9:          RadGrid1.ExportSettings.HideStructureColumns = False
Line 10:         RadGrid1.ExportSettings.IgnorePaging = True
Line 11:         RadGrid1.MasterTableView.ExportToExcel()
Line 12:     End Sub
Line 13: End Class
0
Mira
Telerik team
answered on 16 Dec 2011, 06:08 PM
Hello,

Please try binding the grid using Advanced Data-binding (using NeedDataSource event) and let me know whether it works.

I am looking forward to your reply.

Kind regards,
Mira
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Web Services
Top achievements
Rank 2
answered on 21 Dec 2011, 04:16 PM
Adding the sql data source in my need data source event still doesn't give me the column headers. I'm posting my full file

aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Reporting.aspx.vb" Inherits="Reporting" %>
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%@ Register TagName="link" Src="~/Links.ascx" TagPrefix="control" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <control:link ID="linkControl" runat="server" />
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
        <br />
        <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" Visible="false">
<MasterTableView AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<RowIndicatorColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
 
<ExpandCollapseColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
    <Columns>
        <telerik:GridBoundColumn DataField="FirstName" HeaderText="First Name"
            SortExpression="FirstName" UniqueName="FirstName">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="LastName" HeaderText="Last Name"
            SortExpression="LastName" UniqueName="LastName">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Address" HeaderText="Address"
            SortExpression="Address" UniqueName="Address">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="City" HeaderText="City"
            SortExpression="City" UniqueName="City">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="State" HeaderText="State"
            SortExpression="State" UniqueName="State">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Zip" HeaderText="Zip" SortExpression="Zip"
            UniqueName="Zip">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Phone" HeaderText="Phone"
            SortExpression="Phone" UniqueName="Phone">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Email" HeaderText="Email"
            SortExpression="Email" UniqueName="Email">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="DateFor" HeaderText="Date For"
            SortExpression="DateFor" UniqueName="DateFor">
        </telerik:GridBoundColumn>
    </Columns>
</MasterTableView>
        </telerik:RadGrid>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:connection %>"
            SelectCommand="SELECT [FirstName], [LastName], [Address], [City], [State], [Zip], [Phone], [Email], [DateFor] FROM [People] ORDER BY LastName">
        </asp:SqlDataSource>
        <asp:Button ID="download" runat="server" Text="Download Report" />
    </div>
    </form>
</body>
</html>



vb
Imports Telerik.Web.UI
Partial Class Reporting
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        RadGrid1.DataBind()
 
    End Sub
 
    Protected Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
        Dim dataSource As New SqlDataSource(System.Configuration.ConfigurationManager.ConnectionStrings("connection").ToString, _
                                            "SELECT [FirstName], [LastName], [Address], [City], [State], [Zip], [Phone], [Email], [DateFor] FROM [People] ORDER BY LastName")
        dataSource.DataBind()
 
        RadGrid1.DataSource = dataSource
    End Sub
 
    Protected Sub download_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles download.Click
        RadGrid1.ExportSettings.OpenInNewWindow = True
        RadGrid1.ExportSettings.ExportOnlyData = False
        RadGrid1.ExportSettings.HideStructureColumns = False
        RadGrid1.ExportSettings.IgnorePaging = True
        RadGrid1.ExportSettings.FileName = "Rowe Sanctuary Report"
        RadGrid1.MasterTableView.ExportToExcel()
    End Sub
End Class
0
Mira
Telerik team
answered on 22 Dec 2011, 04:56 PM
Hello,

Please try removing the declarative assignment of the datasource of the grid:
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" Visible="false">
<MasterTableView AutoGenerateColumns="False" DataSourceID="SqlDataSource1">

I hope this helps.

Greetings,
Mira
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Web Services
Top achievements
Rank 2
answered on 23 Dec 2011, 11:29 PM
OK, this is starting to irritate me (not you guys, I appreciate the help!). I know I'm probably doing something stupid and that's my issue, but I still can't get this to show the headers. Here's what my full code is changed to. I can send the the whole project if you like, it's not that big. The strange thing is, I copied this code from another website where I'm exporting to excel and the headers show fine in that one.

aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Reporting.aspx.vb" Inherits="Reporting" %>
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%@ Register TagName="link" Src="~/Links.ascx" TagPrefix="control" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <control:link ID="linkControl" runat="server" />
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
        <br />
        <telerik:RadGrid ID="RadGrid1" runat="server" Visible="false">
<MasterTableView AutoGenerateColumns="False">
<RowIndicatorColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
 
<ExpandCollapseColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
    <Columns>
        <telerik:GridBoundColumn DataField="FirstName" HeaderText="First Name"
            SortExpression="FirstName" UniqueName="FirstName">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="LastName" HeaderText="Last Name"
            SortExpression="LastName" UniqueName="LastName">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Address" HeaderText="Address"
            SortExpression="Address" UniqueName="Address">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="City" HeaderText="City"
            SortExpression="City" UniqueName="City">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="State" HeaderText="State"
            SortExpression="State" UniqueName="State">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Zip" HeaderText="Zip" SortExpression="Zip"
            UniqueName="Zip">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Phone" HeaderText="Phone"
            SortExpression="Phone" UniqueName="Phone">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Email" HeaderText="Email"
            SortExpression="Email" UniqueName="Email">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="DateFor" HeaderText="Date For"
            SortExpression="DateFor" UniqueName="DateFor">
        </telerik:GridBoundColumn>
    </Columns>
</MasterTableView>
        </telerik:RadGrid>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:connection %>"
            SelectCommand="SELECT [FirstName], [LastName], [Address], [City], [State], [Zip], [Phone], [Email], [DateFor] FROM [People] ORDER BY LastName">
        </asp:SqlDataSource>
        <asp:Button ID="download" runat="server" Text="Download Report" />
    </div>
    </form>
</body>
</html>



vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    RadGrid1.DataBind()
 
End Sub
 
Protected Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
    Dim dataSource As New SqlDataSource(System.Configuration.ConfigurationManager.ConnectionStrings("connection").ToString, _
                                        "SELECT [FirstName], [LastName], [Address], [City], [State], [Zip], [Phone], [Email], [DateFor] FROM [People] ORDER BY LastName")
    dataSource.DataBind()
 
    RadGrid1.DataSource = dataSource
End Sub
 
Protected Sub download_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles download.Click
    RadGrid1.ExportSettings.OpenInNewWindow = True
    RadGrid1.ExportSettings.ExportOnlyData = False
    RadGrid1.ExportSettings.HideStructureColumns = False
    RadGrid1.ExportSettings.IgnorePaging = True
    RadGrid1.ExportSettings.FileName = "Rowe Sanctuary Report"
    RadGrid1.MasterTableView.ExportToExcel()
End Sub





0
Daniel
Telerik team
answered on 28 Dec 2011, 09:32 AM
Hello,

The problem is that you are trying to export invisible RadGrid. Modify the code in the button handler as shown in the code-block below.
Also I apologize for the discrepancy, but I believe it would be better to stick to the previous approach (e.g. declarative datasource) instead of your current one.

Protected Sub download_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles download.Click
    RadGrid1.ExportSettings.OpenInNewWindow = True
    RadGrid1.ExportSettings.ExportOnlyData = False
    RadGrid1.ExportSettings.HideStructureColumns = False
    RadGrid1.ExportSettings.IgnorePaging = True
    RadGrid1.ExportSettings.FileName = "Rowe Sanctuary Report"
    RadGrid1.Visible = True
    RadGrid1.MasterTableView.ExportToExcel()
End Sub

Note that you may need to invoke RadGrid1.DataBind after setting the Visible property.
I hope this helps.

Regards,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
nick chance
Top achievements
Rank 1
answered on 24 Oct 2012, 11:52 PM
USING RadGrid from 2012 Q2 NET 35
OS = Windows 7 64 Bit Ultimate other computer info on attached file.

Defining the Grid as follows:-
<telerik:RadGrid ID="rdgReportGrid1" runat="server" AutoGenerateColumns="False"
CellSpacing="0" GridLines="None" Visible="True"
OnItemCommand="rdgReportGrid1_ItemCommand" ShowStatusBar="true">
<ExportSettings HideStructureColumns="true">
</ExportSettings>
<MasterTableView Width="100%" CommandItemDisplay="Top" Caption="Report Grid 1">
<PagerStyle Mode="NextPrevNumericAndAdvanced"></PagerStyle>
<CommandItemSettings ShowExportToWordButton="true" ShowExportToExcelButton="true"
ShowExportToCsvButton="true" ShowAddNewRecordButton="false" ShowRefreshButton="false">
</CommandItemSettings>
</MasterTableView>
</telerik:RadGrid>

and the VB Code looks like this:-
Private Sub DispTable(ByVal myTable As DataTable, ByVal myGrid As RadGrid)
      
     Dim dColumn     As System.Data.DataColumn
     Dim radColumn   As  object
 
     myGrid.Columns.Clear()
     For Each dColumn In myTable.Columns
         Dim SB As New  System.Text.StringBuilder()
         SB.Append (dColumn.ColumnName)
         Dim arrFred(0) As  string
         arrFred(0) = dColumn.ColumnName.tostring
 
         If Left(dColumn.ColumnName, 4) =  "IMG_" Then
             radColumn                                               = New Telerik.Web.UI.GridImageColumn
             myGrid.MasterTableView.Columns.Add(radColumn)   
             CType(radColumn, GridImageColumn).DataImageUrlFields    = arrFred
             CType(radColumn, GridImageColumn).ImageHeight           = 30
             CType(radColumn, GridImageColumn).ImageWidth            = 30
             CType(radColumn, GridImageColumn).ImageAlign            = ImageAlign.Middle
             radColumn.HeaderText                                    = Mid(dColumn.ColumnName,5)
 
         ElseIf Left(dColumn.ColumnName, 3) =  "HL_" Then
             radColumn           = New GridHyperLinkColumn
             myGrid.MasterTableView.Columns.Add(radColumn)   
              
             CType(radColumn, GridHyperLinkColumn).DataNavigateUrlFields = arrFred ' eg HGL_VIN field
             CType(radColumn, GridHyperLinkColumn).DataTextField     = Mid(dColumn.ColumnName,4) & "ID" ' eg other VIN field
             CType(radColumn, GridHyperLinkColumn).Target            = "_blank"
             radColumn.HeaderText= Mid(dColumn.ColumnName,4)
         Else
             radColumn                                               = New GridBoundColumn
             myGrid.MasterTableView.Columns.Add(radColumn)   
             radColumn.DataField                                     = dColumn.ColumnName
             radColumn.HeaderText                                    = dColumn.ColumnName
         End If
         radColumn.UniqueName                                        = SB.Replace(" ", "_").ToString
         radColumn.Visible                                           = CBool(InStr(dColumn.ColumnName.ToString,"ID") = 0)
     Next
 
     myGrid.AutoGenerateColumns  = False
     myGrid.DataSource           = myTable
     myGrid.DataBind()
     myGrid.Visible              = True
 
 End Sub

Also attached is image of grid with headers displayed perfectly OK.
Also attached is the result of clicking on the XML button on the Grid - No Column header but Caption OK. ???
Although I operated with the Grids Visible = False after seeing the comment above I made them visible = true, but still  no Headings.

Anyone got a clue as to what I am doing incorrectly?
Been struggling with this for two days and just about at my wits end.


0
Kostadin
Telerik team
answered on 29 Oct 2012, 03:10 PM
Hi Nick,

Could you try to call myGrid.DataBind() after setting the Visibile property of the grid to true?  
myGrid.AutoGenerateColumns  = False
myGrid.DataSource           = myTable
myGrid.Visible = True
myGrid.DataBind()  

I hope this helps.

Regards,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
nick chance
Top achievements
Rank 1
answered on 30 Oct 2012, 01:20 PM
Hi Kostadin - Thanks for replying and sorry I haven't been able to try this out earlier (travelling back to the UK from Aus). OK - I tried this out as follows:-

 

myGrid.AutoGenerateColumns  = False
myGrid.DataSource           = myTable
myGrid.Visible              = True
myGrid.DataBind()

and I am afraid I am still not getting the Excel headings. This is getting a bit desperate now and the client is losing patience. Maybe the only way is to populate the Excel Sheet programmatically, bit slow but at least I will get the headings.

Nick

 

 

 

 


0
Kostadin
Telerik team
answered on 02 Nov 2012, 12:50 PM
Hello Nick,

I was not able to reproduce the issue. Could you specify what kind of databinding you are using? Is it an Advanced DataBinding or a Simple DataBinding? If you are using simple databinding could you try to use the advanced databinding?

Kind regards,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Web Services
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Web Services
Top achievements
Rank 2
Mira
Telerik team
Daniel
Telerik team
nick chance
Top achievements
Rank 1
Kostadin
Telerik team
Share this question
or