Hi,
I have a RadGrid with a TempleColumn and I have some problem exporting the grid to excel, the context is as follow:
The RadGrid is bound from a stored procedure in a data base, no problem here... the real difficult is when I want to export this grid to an Excel format, the grid has a template column and the data from the template never goes to exported excel file (see attachments).
The grid is created programatically and one of the columns is a template column. The grid needs to be created programatically because the columns returned for the stored procedure are always variable (dynamics) and I need generated a templated column (some merge headers) based on these results.
Any suggestions ?? Thanks in advance to all for your help
I have a RadGrid with a TempleColumn and I have some problem exporting the grid to excel, the context is as follow:
The RadGrid is bound from a stored procedure in a data base, no problem here... the real difficult is when I want to export this grid to an Excel format, the grid has a template column and the data from the template never goes to exported excel file (see attachments).
The grid is created programatically and one of the columns is a template column. The grid needs to be created programatically because the columns returned for the stored procedure are always variable (dynamics) and I need generated a templated column (some merge headers) based on these results.
Any suggestions ?? Thanks in advance to all for your help
This is the code in the .aspx page
<
asp:Button
ID
=
"Button1"
runat
=
"server"
Text
=
"Exportar a Excel"
onclick
=
"Button1_Click"
/>
<
br
/><
br
/>
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
>
</
telerik:RadGrid
>
//This is the .aspx.cs code
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(!IsPostBack)
{
ds = obj_concentrador.ALE_ConcentradorLogros_Select(
"7"
,
"2011"
,
"6"
,
"1"
,
"1"
);
RadGrid1.DataSource = ds.Tables[0].DefaultView;
RadGrid1.MasterTableView.DataKeyNames =
new
string
[] {
"Alum_Id"
};
RadGrid1.ExportSettings.HideStructureColumns =
false
;
RadGrid1.ExportSettings.ExportOnlyData =
false
;
RadGrid1.ExportSettings.IgnorePaging =
true
;
RadGrid1.ExportSettings.OpenInNewWindow =
true
;
RadGrid1.ExportSettings.FileName =
"Concentrador"
;
//RadGrid1.ExportSettings.Excel.Format = GridExcelExportFormat.Html;
RadGrid1.Width = Unit.Percentage(98);
RadGrid1.PageSize = 5;
RadGrid1.AllowPaging =
true
;
RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
RadGrid1.AutoGenerateColumns =
false
;
RadGrid1.ShowHeader =
true
;
RadGrid1.ShowStatusBar =
true
;
//Add Customers table
RadGrid1.MasterTableView.PageSize = 50;
RadGrid1.MasterTableView.Width = Unit.Percentage(100);
RadGrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
RadGrid1.MasterTableView.CommandItemSettings.ShowExportToWordButton =
false
;
RadGrid1.MasterTableView.CommandItemSettings.ShowExportToExcelButton =
true
;
RadGrid1.MasterTableView.CommandItemSettings.ExportToExcelText =
"Exportar a Excel"
;
RadGrid1.MasterTableView.CommandItemSettings.ShowExportToPdfButton =
true
;
RadGrid1.MasterTableView.CommandItemSettings.ExportToPdfText =
"Exportar a Pdf"
;
RadGrid1.MasterTableView.CommandItemSettings.ShowExportToCsvButton =
false
;
RadGrid1.ItemDataBound +=
new
GridItemEventHandler(RadGrid1_ItemDataBound);
GridBoundColumn boundColumn1;
boundColumn1 =
new
GridBoundColumn();
RadGrid1.MasterTableView.Columns.Add(boundColumn1);
boundColumn1.DataField =
"Alum_Id"
;
boundColumn1.HeaderText =
"Alum_Id"
;
boundColumn1.Visible =
false
;
GridBoundColumn boundColumn2;
boundColumn2 =
new
GridBoundColumn();
RadGrid1.MasterTableView.Columns.Add(boundColumn2);
boundColumn2.DataField =
"Alum_NomApellidos"
;
boundColumn2.HeaderText =
"Estudiante"
;
boundColumn2.ItemStyle.Width = Unit.Percentage(20);
string
templateColumnName =
"materiaName"
;
string
templateColumnLogros =
"logrosName"
;
GridTemplateColumn templateColum;
templateColum =
new
GridTemplateColumn();
RadGrid1.MasterTableView.Columns.Add(templateColum);
templateColum.ItemStyle.Width = Unit.Percentage(80);
templateColum.ItemTemplate =
new
MyTemplate(templateColumnName, ds);
templateColum.HeaderTemplate =
new
MyTemplate2(templateColumnLogros, ds);
templateColum.UniqueName =
"TemplateColumn"
;
templateColum.InitializeTemplatesFirst =
false
;
}
}
public
class
MyTemplate : ITemplate
{
private
string
colname;
protected
Label lControl;
private
DataSet ds;
public
MyTemplate(
string
cName, DataSet datos)
{
colname = cName;
ds = datos;
}
public
void
InstantiateIn(System.Web.UI.Control container)
{
lControl =
new
Label();
lControl.ID =
"lControl"
;
lControl.DataBinding +=
new
EventHandler(lControl_DataBinding);
container.Controls.Add(lControl);
}
public
void
lControl_DataBinding(
object
sender, EventArgs e)
{
Label l = (Label)sender;
GridDataItem container = (GridDataItem)l.NamingContainer;
string
sItem =
string
.Empty;
sItem =
"<table width='100%' border='0' cellpadding='0' cellspacing='0'><tr>"
;
float
ancho = 80 / (ds.Tables[0].Columns.Count - 2);
for
(
int
i = 2; i < ds.Tables[0].Columns.Count; i++)
{
sItem +=
"<td align='center' style='width:"
+ ancho.ToString() +
"%'>"
+ ((DataRowView)container.DataItem)[ds.Tables[0].Columns[i].ColumnName].ToString() +
"</td>"
;
}
sItem +=
"</tr></table>"
;
l.Text = sItem;
}
}
public
class
MyTemplate2 : ITemplate
{
private
DataSet ds;
private
string
colname;
protected
Label lControl;
public
MyTemplate2(
string
cName, DataSet datos)
{
colname = cName;
ds = datos;
}
public
void
InstantiateIn(System.Web.UI.Control container)
{
lControl =
new
Label();
lControl.ID =
"lControl"
;
lControl.DataBinding +=
new
EventHandler(lControl_DataBinding);
container.Controls.Add(lControl);
}
public
void
lControl_DataBinding(
object
sender, EventArgs e)
{
Label l = (Label)sender;
GridHeaderItem container = (GridHeaderItem)l.NamingContainer;
string
sHeader =
string
.Empty;
sHeader =
"<table id='Table1' width='100%' border='0' cellpadding='0' cellspacing='0'><tr>"
;
sHeader +=
"<td colspan='"
+ (ds.Tables[0].Columns.Count - 2).ToString() +
"' align='center'>Nombres de Materias</td></tr><tr>"
;
float
ancho = 80 / (ds.Tables[0].Columns.Count - 2);
for
(
int
i = 2; i < ds.Tables[0].Columns.Count; i++)
{
sHeader +=
"<td align='center' style='width:"
+ ancho.ToString() +
"%'>"
+ ds.Tables[0].Columns[i].ColumnName +
"</td>"
;
}
sHeader +=
"</tr></table>"
;
l.Text = sHeader;
}
}
protected
void
Button1_Click(
object
sender, EventArgs e)
{
RadGrid1.MasterTableView.Columns.FindByUniqueName(
"TemplateColumn"
).Visible =
true
;
RadGrid1.ExportSettings.ExportOnlyData =
true
;
RadGrid1.ExportSettings.IgnorePaging =
true
;
RadGrid1.ExportSettings.OpenInNewWindow =
true
;
RadGrid1.ExportSettings.FileName =
"Concentrador"
;
RadGrid1.MasterTableView.ExportToExcel();
}