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

exporting a radgrid template column control data to excel

5 Answers 994 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jay
Top achievements
Rank 1
Jay asked on 13 Sep 2012, 02:33 PM
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

 

5 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 13 Sep 2012, 04:43 PM
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
0
Pradip
Top achievements
Rank 1
answered on 26 Nov 2014, 06:23 AM
Hello Jayesh

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

any idea ?
0
Jayesh Goyani
Top achievements
Rank 2
answered on 02 Dec 2014, 04:02 PM
Hi,

Could you please provide your code?     

Thanks,
Jayesh Goyani
0
NLNG Webmasters
Top achievements
Rank 1
answered on 26 Apr 2016, 07:48 AM

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>

0
Kostadin
Telerik team
answered on 28 Apr 2016, 11:06 AM
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.
Tags
Grid
Asked by
Jay
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Pradip
Top achievements
Rank 1
NLNG Webmasters
Top achievements
Rank 1
Kostadin
Telerik team
Share this question
or