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
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
I have tried but its not working with Linkbutton in Item Temlate.
any idea ?
Could you please provide your code?
Thanks,
Jayesh Goyani
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>
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