I have a RadGrid that I'm trying to export to Excel. In the RadGrid, it contains a GridTemplateColumn with nested repeaters, labels and spans. I'm trying to use ExcelML for the export, but am open to using another format if necessary. My telerik version is 2014.1.225.45.
In the ExcelMLExportRowCreated method, I'm trying to find the first repeater control and then traverse down to the labels, the second repeater and it's subsequent labels and span and concatenate the data into a string. I seem to be finding the repeater controls, but each label I find returns the text value as an empty string. I can't seem to find the actual text that shows up in the grid.
I've read a variety of forum threads trying to figure this out. Can someone suggest how I can get the values out of these controls?
Here is my GridItemTemplate Column and the code behind for the export.
Any help would be appreciated.
Thank you
<telerik:GridTemplateColumn UniqueName=
"boards"
DataField=
"boards"
HeaderText=
"Groups/Committees"
ItemStyle-CssClass=
"system-users-committee-list"
>
<ItemTemplate>
<%-- Start List of Boards then nest committees under board list item --%>
<ul
class
=
"board-memberships unstyled"
>
<asp:Repeater ID=
"rptBoards"
runat=
"server"
DataSource =
'<%# Eval("boards") %>'
>
<ItemTemplate>
<li
class
=
"multi-member-board"
>
<li
class
=
"board-name"
>
<asp:Label ID=
"lblBoardName"
runat=
"server"
><%# Eval(
"boardName"
) +
":"
%></asp:Label>
<ul
class
=
"committees unstyled inline"
>
<asp:Repeater ID=
"rptGroups"
runat=
"server"
DataSource =
'<%# Eval("groups") %>'
OnItemDataBound=
"rptrCommittee_ItemDataBound"
>
<ItemTemplate>
<li>
<asp:Label ID=
"lblCommitteeList"
runat=
"server"
><%# Eval(
"groupName"
) %></asp:Label>
<!--
if
role -->
<span id=
"roleName"
class
=
"role"
runat=
"server"
visible=
"false"
>(<%# Eval(
"role.roleName"
) %>)</span>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</li>
</li>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</telerik:GridTemplateColumn>
protected
void
btnExportExcel_Click(
object
sender, EventArgs e)
{
radGridSysDir.ExportSettings.ExportOnlyData = True
radGridSysDir.ExportSettings.IgnorePaging =
true
;
radGridSysDir.ExportSettings.FileName =
string
.Format(
"SystemDirectoryExportExcel_{0}"
, DateTime.Today);
radGridSysDir.ExportSettings.Excel.Format = GridExcelExportFormat.ExcelML;
isExport =
true
;
radGridSysDir.MasterTableView.ExportToExcel();
}
protected
void
radGridSysDir_ExcelMLExportRowCreated(
object
sender, Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLRowCreatedArgs e)
{
if
(e.RowType == GridExportExcelMLRowType.HeaderRow)
{
radGridSysDir.Rebind();
e.Worksheet.Table.Columns.Add(
new
ColumnElement());
CellElement cell =
new
CellElement();
cell.ColumnName =
"MemberName"
;
cell.Data.DataItem =
"Member Name"
;
e.Row.Cells.Add(cell);
e.Worksheet.Table.Columns.Add(
new
ColumnElement());
CellElement cell2 =
new
CellElement();
cell2.ColumnName =
"BoardName"
;
cell2.Data.DataItem =
"Board Name"
;
e.Row.Cells.Add(cell2);
}
if
(e.RowType == GridExportExcelMLRowType.DataRow)
{
CellElement cell =
new
CellElement();
int
currentRow = e.Worksheet.Table.Rows.IndexOf(e.Row) - 1;
// cell = e.Row.Cells.GetCellByName("TemplateColumn");
cell.Data.DataItem = (radGridSysDir.MasterTableView.Items[currentRow].FindControl(
"lblName"
)
as
Label).Text;
e.Row.Cells.Add(cell);
GridDataItem item = radGridSysDir.MasterTableView.Items[currentRow];
StringBuilder newString =
new
StringBuilder();
CellElement cell2 =
new
CellElement();
Repeater rptBoards = (Repeater)item.FindControl(
"rptBoards"
);
foreach
(RepeaterItem rItem
in
rptBoards.Items)
{
if
(rItem.ItemType == ListItemType.Item || rItem.ItemType == ListItemType.AlternatingItem)
{
Label lblBname = (Label)rItem.Controls[1].FindControl(
"lblBoardName"
);
Repeater rptGroups = (Repeater)rItem.FindControl(
"rptGroups"
);
newString.Append(lblBname.Text);
foreach
(RepeaterItem gItem
in
rptGroups.Items)
{
if
(gItem.ItemType == ListItemType.Item || gItem.ItemType == ListItemType.AlternatingItem)
{
Label lblGrp = (Label)gItem.Controls[0].FindControl(
"lblCommitteeList"
);
if
(lblGrp !=
null
)
{
HtmlGenericControl spanRole = (HtmlGenericControl)gItem.Controls[1].FindControl(
"roleName"
);
if
(spanRole !=
null
)
{
newString.Append(lblGrp.Text);
newString.Append(spanRole.ToString());
}
}
}
}
}
}
cell2.Data.DataItem = newString.ToString();
e.Row.Cells.Add(cell2);
}
}