exporting a radgrid template column control data to excel

6 posts, 0 answers
  1. Jay
    Jay avatar
    19 posts
    Member since:
    Dec 2011

    Posted 13 Sep 2012 Link to this post

    Hi,

     

    in my radgrid i have three template columns, first column textbox, second dropdown list, third datecontrol

     

     

    when i click on export to excel, i need to export the data in Template columns to excel

     

    data

    in bound columns are getting exported to excel, problem is only with template columns

     

    can

    any one help me how to achieve this

    Thank you in advance

     

  2. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 13 Sep 2012 Link to this post

    Hello,

    Please try with below code snippet.

    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" OnNeedDataSource="RadGrid1_NeedDataSource"
               OnItemCommand="RadGrid1_ItemCommand">
               <ExportSettings ExportOnlyData="true">
               </ExportSettings>
               <MasterTableView DataKeyNames="ID" CommandItemDisplay="Top">
                   <CommandItemSettings ShowExportToExcelButton="true" />
                   <Columns>
                       <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID">
                       </telerik:GridBoundColumn>
                       <telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name">
                       </telerik:GridBoundColumn>
                       <telerik:GridTemplateColumn UniqueName="FirstName">
                           <ItemTemplate>
                               <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                           </ItemTemplate>
                       </telerik:GridTemplateColumn>
                   </Columns>
               </MasterTableView>
           </telerik:RadGrid>
        protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
           {
               if (e.CommandName == RadGrid.ExportToExcelCommandName)
               {
                   foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
                   {
                       item["FirstName"].Text = (item.FindControl("TextBox1") as TextBox).Text;
                   }
               }
           }
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
           {
               dynamic data = new[] {
                 new { ID = 1, Name ="aaa"},
                 new { ID = 2, Name = "bbb"},
                 new { ID = 3, Name = "ccc"},
                 new { ID = 4, Name = "ddd"},
                  new { ID = 5, Name ="eee"}
               };
               RadGrid1.DataSource = data;
           }


    Thanks,
    Jayesh Goyani
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Pradip
    Pradip avatar
    9 posts
    Member since:
    Nov 2011

    Posted 26 Nov 2014 in reply to Jayesh Goyani Link to this post

    Hello Jayesh

    I have tried but its not working with Linkbutton in Item Temlate.

    any idea ?
  5. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 02 Dec 2014 in reply to Pradip Link to this post

    Hi,

    Could you please provide your code?     

    Thanks,
    Jayesh Goyani
  6. NLNG Webmasters
    NLNG Webmasters avatar
    1 posts
    Member since:
    Aug 2009

    Posted 26 Apr Link to this post

    Hello i am also having a similar issue. I am trying to export a grid with Template column and it is always coming up with "Object reference not set to an instance of an object." error upon clicking the Export to Excel button on  the Command Item.

    <telerik:RadGrid ID="RadGrid1" runat="server" AllowSorting="True" DataSourceID="DivisionDS" GroupPanelPosition="Top" ResolvedRenderMode="Classic"
                onitemdatabound="RadGrid1_ItemDataBound" onitemcommand="RadGrid1_ItemCommand">
                <ExportSettings ExportOnlyData="True" HideStructureColumns="True" IgnorePaging="True" OpenInNewWindow="True">
                </ExportSettings>
                <MasterTableView AutoGenerateColumns="False" DataKeyNames="Division" DataSourceID="DivisionDS" CommandItemDisplay="Top">
                    <CommandItemSettings ShowAddNewRecordButton="False" ShowExportToExcelButton="True" />
                    <RowIndicatorColumn Visible="False">
                    </RowIndicatorColumn>
                    <ExpandCollapseColumn Created="True">
                    </ExpandCollapseColumn>
                    <Columns>
                        <telerik:GridBoundColumn DataField="Division" FilterControlAltText="Filter Division column" HeaderText="Division" ReadOnly="True" SortExpression="Division" UniqueName="Division"/>
                        <telerik:GridTemplateColumn HeaderText="Department" SortExpression="Department" UniqueName="Department">
                            <ItemTemplate>
                                <telerik:RadGrid ID="rgDepartment" runat="server" AllowSorting="True" GroupPanelPosition="Top" ResolvedRenderMode="Classic">
                                    <MasterTableView AutoGenerateColumns="False" DataKeyNames="Department">
                                        <Columns>
                                            <telerik:GridBoundColumn DataField="Department" FilterControlAltText="Filter Department column" HeaderText="Department" ReadOnly="True" SortExpression="Department" UniqueName="Department"/>
                                            <telerik:GridTemplateColumn HeaderText="No Of Guest" SortExpression="GuestNo" UniqueName="GuestNo">
                                                <ItemTemplate>
                                                    <asp:Label ID="lbGuest" runat="server"/>
                                                </ItemTemplate>
                                            </telerik:GridTemplateColumn>
                                            <telerik:GridTemplateColumn HeaderText="Room Nite" SortExpression="RoomNite" UniqueName="RoomNite">
                                                <ItemTemplate>
                                                    <asp:Label ID="lbRoomNite" runat="server"/>
                                                </ItemTemplate>
                                            </telerik:GridTemplateColumn>
                                            <telerik:GridTemplateColumn HeaderText="Hotel" SortExpression="Hotel" UniqueName="Hotel">
                                                <ItemTemplate>
                                                    <telerik:RadGrid ID="rgHotel" runat="server" AllowSorting="True" GroupPanelPosition="Top" ResolvedRenderMode="Classic">
                                                        <MasterTableView AutoGenerateColumns="False" DataKeyNames="BookingID">
                                                            <Columns>
                                                                <telerik:GridBoundColumn DataField="BookingID" DataType="System.Int32" FilterControlAltText="Filter BookingID column" HeaderText="BookingID" ReadOnly="True" SortExpression="BookingID" UniqueName="BookingID" Display="false"/>
                                                                <telerik:GridBoundColumn DataField="HotelID" DataType="System.Int32" FilterControlAltText="Filter HotelID column" HeaderText="HotelID" SortExpression="HotelID" UniqueName="HotelID" Display="false"/>
                                                                <telerik:GridBoundColumn DataField="Hotel" FilterControlAltText="Filter Hotel column" HeaderText="Hotel" ReadOnly="True" SortExpression="Hotel" UniqueName="Hotel"/>
                                                                <telerik:GridTemplateColumn HeaderText="Room Nite" SortExpression="HotelRoomNite" UniqueName="HotelRoomNite">
                                                                    <ItemTemplate>
                                                                        <asp:Label ID="lbHotelRoomNite" runat="server"/>
                                                                    </ItemTemplate>
                                                                </telerik:GridTemplateColumn>
                                                            </Columns>
                                                        </MasterTableView>
                                                    </telerik:RadGrid>
                                                </ItemTemplate>
                                            </telerik:GridTemplateColumn>
                                        </Columns>
                                    </MasterTableView>
                                </telerik:RadGrid>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                    </Columns>
                </MasterTableView>
            </telerik:RadGrid>

  7. Kostadin
    Admin
    Kostadin avatar
    1708 posts

    Posted 28 Apr Link to this post

    Hello,

    In order to export the data of the TemplateColumns you need to disable both ExportOnlyData and IgnorePaging properties. If you need to export all the data then you can disable the paging and rebind the grid when the export command is fired.
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.ExportToExcelCommandName)
        {
            RadGrid1.AllowPaging = false;
            RadGrid1.Rebind();
        }
    }

    Regards,
    Kostadin
    Telerik
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017