Column headings not showing on export

14 posts, 0 answers
  1. Web Services
    Web Services avatar
    243 posts
    Member since:
    Apr 2008

    Posted 13 Dec 2011 Link to this post

    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()
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 13 Dec 2011 Link to this post

    Hello,

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

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Web Services
    Web Services avatar
    243 posts
    Member since:
    Apr 2008

    Posted 14 Dec 2011 Link to this post

    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


  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 14 Dec 2011 Link to this post

    Hello,

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

    Thanks,
    Princy.
  6. Web Services
    Web Services avatar
    243 posts
    Member since:
    Apr 2008

    Posted 16 Dec 2011 Link to this post

    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
  7. Mira
    Admin
    Mira avatar
    1124 posts

    Posted 16 Dec 2011 Link to this post

    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
  8. Web Services
    Web Services avatar
    243 posts
    Member since:
    Apr 2008

    Posted 21 Dec 2011 Link to this post

    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
  9. Mira
    Admin
    Mira avatar
    1124 posts

    Posted 22 Dec 2011 Link to this post

    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
  10. Web Services
    Web Services avatar
    243 posts
    Member since:
    Apr 2008

    Posted 23 Dec 2011 Link to this post

    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





  11. Daniel
    Admin
    Daniel avatar
    4946 posts

    Posted 28 Dec 2011 Link to this post

    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
  12. nick chance
    nick chance avatar
    6 posts
    Member since:
    Oct 2012

    Posted 24 Oct 2012 Link to this post

    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.


  13. Kostadin
    Admin
    Kostadin avatar
    1713 posts

    Posted 29 Oct 2012 Link to this post

    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.
  14. nick chance
    nick chance avatar
    6 posts
    Member since:
    Oct 2012

    Posted 30 Oct 2012 Link to this post

    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

     

     

     

     


  15. Kostadin
    Admin
    Kostadin avatar
    1713 posts

    Posted 02 Nov 2012 Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017