I have read several posts regarding this topic and have not really found a good answer. I have a datagrid that I need to add some additional columns when I export the data (Excel, CSV, PDF, Word) and populate with some extra data. I have tried adding the columns on the GridExporting and ItemCommand events but it doesn't work. I can't do this in the ItemDatabound event because I won't have the extra data at this time. Does anyone have any suggestions?
4 Answers, 1 is accepted
0

Jayesh Goyani
Top achievements
Rank 2
answered on 09 Jul 2013, 06:08 AM
Hello,
Using Display Property
Using Visible property
Thanks,
Jayesh Goyani
Using Display Property
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
AutoGenerateColumns
=
"false"
OnNeedDataSource
=
"RadGrid1_NeedDataSource"
OnItemCommand
=
"RadGrid1_ItemCommand"
>
<
ExportSettings
OpenInNewWindow
=
"true"
>
</
ExportSettings
>
<
MasterTableView
DataKeyNames
=
"ID"
CommandItemDisplay
=
"Top"
>
<
CommandItemSettings
ShowExportToPdfButton
=
"true"
ShowExportToExcelButton
=
"true"
ShowExportToCsvButton
=
"true"
/>
<
Columns
>
<
telerik:GridBoundColumn
DataField
=
"ID"
UniqueName
=
"ID"
HeaderText
=
"ID"
Display
=
"false"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"Name"
UniqueName
=
"Name"
HeaderText
=
"Name"
>
</
telerik:GridBoundColumn
>
<
telerik:GridEditCommandColumn
>
</
telerik:GridEditCommandColumn
>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
protected
void
RadGrid1_NeedDataSource(
object
sender, GridNeedDataSourceEventArgs e)
{
dynamic data =
new
[] {
new
{ ID = 1, Name =
"Name1"
},
new
{ ID = 2, Name =
"Name2"
},
new
{ ID = 3, Name =
"Name3"
},
new
{ ID = 4, Name =
"Name4"
},
new
{ ID = 5, Name =
"Name5"
}
};
RadGrid1.DataSource = data;
}
protected
void
RadGrid1_ItemCommand(
object
sender, GridCommandEventArgs e)
{
if
(e.CommandName == RadGrid.ExportToPdfCommandName ||
e.CommandName == RadGrid.ExportToExcelCommandName ||
e.CommandName == RadGrid.ExportToCsvCommandName)
{
if
(RadGrid1.MasterTableView.Columns.FindByUniqueName(
"ID"
) !=
null
)
{
RadGrid1.MasterTableView.Columns.FindByUniqueName(
"ID"
).Display =
true
;
}
}
}
Using Visible property
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
AutoGenerateColumns
=
"false"
OnNeedDataSource
=
"RadGrid1_NeedDataSource"
OnItemCommand
=
"RadGrid1_ItemCommand"
>
<
ExportSettings
OpenInNewWindow
=
"true"
>
</
ExportSettings
>
<
MasterTableView
DataKeyNames
=
"ID"
CommandItemDisplay
=
"Top"
>
<
CommandItemSettings
ShowExportToPdfButton
=
"true"
ShowExportToExcelButton
=
"true"
ShowExportToCsvButton
=
"true"
/>
<
Columns
>
<
telerik:GridBoundColumn
DataField
=
"ID"
UniqueName
=
"ID"
HeaderText
=
"ID"
Visible
=
"false"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"Name"
UniqueName
=
"Name"
HeaderText
=
"Name"
>
</
telerik:GridBoundColumn
>
<
telerik:GridEditCommandColumn
>
</
telerik:GridEditCommandColumn
>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
protected
void
RadGrid1_NeedDataSource(
object
sender, GridNeedDataSourceEventArgs e)
{
dynamic data =
new
[] {
new
{ ID = 1, Name =
"Name1"
},
new
{ ID = 2, Name =
"Name2"
},
new
{ ID = 3, Name =
"Name3"
},
new
{ ID = 4, Name =
"Name4"
},
new
{ ID = 5, Name =
"Name5"
}
};
RadGrid1.DataSource = data;
}
protected
void
RadGrid1_ItemCommand(
object
sender, GridCommandEventArgs e)
{
if
(e.CommandName == RadGrid.ExportToPdfCommandName ||
e.CommandName == RadGrid.ExportToExcelCommandName ||
e.CommandName == RadGrid.ExportToCsvCommandName)
{
if
(RadGrid1.MasterTableView.Columns.FindByUniqueName(
"ID"
) !=
null
)
{
RadGrid1.MasterTableView.Columns.FindByUniqueName(
"ID"
).Visible =
true
;
}
RadGrid1.Rebind();
}
}
Thanks,
Jayesh Goyani
0

dhuss
Top achievements
Rank 1
answered on 09 Jul 2013, 02:07 PM
The only thing I see that is a problem for me is the additional columns will be in the grid when ever the NeedDataSource event fires. I only want the new columns when I export, other than that, the additional columns should not be in the grid. Wouldn't this error out if you don't remove the additional columns when a second NeedDataSource event fires, it would seem that you would end up with multiple columns with the same ID's.
Also, with the newest versions of the Telerik.web.dll (breaking changes in Q1 2013 Release), the bound grid columns will not load with data if the visible property is used. So even if you make the columns visible in the ItemCommand event, there won't be any data in the column. Display only property for me
Also, with the newest versions of the Telerik.web.dll (breaking changes in Q1 2013 Release), the bound grid columns will not load with data if the visible property is used. So even if you make the columns visible in the ItemCommand event, there won't be any data in the column. Display only property for me
0
Accepted
Hello Dennis,
Jayesh suggestion is to hide the columns by setting Display property to false to them and show them only when export command is fired. Note that the column will be shown only when an export command is fired and in all other cases will be hidden. Could you please give this suggestion a try and let me know about the result?
Regards,
Kostadin
Telerik
Jayesh suggestion is to hide the columns by setting Display property to false to them and show them only when export command is fired. Note that the column will be shown only when an export command is fired and in all other cases will be hidden. Could you please give this suggestion a try and let me know about the result?
Regards,
Kostadin
Telerik
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 the blog feed now.
0

dhuss
Top achievements
Rank 1
answered on 11 Jul 2013, 03:11 PM
Not all of our users export their data and we didn't want to changed the stored procedures because this would affect all of our users. Our main concern was what type of performance issue would we have with our stored procedures. After some testing we found it to not be an issue so we reworked the stored procedures to include the additional columns that were needed for export. The additional grid columns are set to display = false and are reset to display = true at export time.
Thanks
Thanks