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

RadGrid - Problem when export with template column

5 Answers 254 Views
Grid
This is a migrated thread and some comments may be shown as answers.
oscarcuestameza
Top achievements
Rank 1
oscarcuestameza asked on 04 Apr 2011, 05:17 PM
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

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();
    }

5 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 07 Apr 2011, 10:25 PM
Hello Oscar,

Can you please try to reproduce the problem after applying the modifications that I have suggested in the support ticket?

Regards,
Daniel
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Nicolaï
Top achievements
Rank 2
answered on 21 Aug 2013, 10:49 AM
Hello,

Any tips? What was suggested?
0
Princy
Top achievements
Rank 2
answered on 21 Aug 2013, 12:46 PM
Hi Nicolai,

Please try the below code snippet to export template column to excel.

ASPX:
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

C#:
protected void Page_Init(object sender, EventArgs e)
{
    RadGrid RadGrid1;
    RadGrid1 = new RadGrid();
    RadGrid1.DataSourceID = "SqlDataSource1";
    RadGrid1.MasterTableView.DataKeyNames = new string[] { "Orderid" };
 
 
    RadGrid1.ExportSettings.ExportOnlyData = true;
    RadGrid1.ExportSettings.IgnorePaging = true;
    RadGrid1.ExportSettings.OpenInNewWindow = true;
    RadGrid1.ExportSettings.FileName = "radgrid";
    RadGrid1.ExportSettings.Excel.Format = GridExcelExportFormat.Html;
 
 
    RadGrid1.PageSize = 5;
    RadGrid1.AllowPaging = true;
    RadGrid1.AutoGenerateColumns = false;
    RadGrid1.ShowHeader = true;
 
    RadGrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
    RadGrid1.MasterTableView.CommandItemSettings.ShowExportToExcelButton = true;
 
    GridBoundColumn boundColumn1;
    boundColumn1 = new GridBoundColumn();  
    RadGrid1.MasterTableView.Columns.Add(boundColumn1);   
    boundColumn1.DataField = "orderid";
    boundColumn1.HeaderText = "orderid"
 
    GridBoundColumn boundColumn2;
    boundColumn2 = new GridBoundColumn();
    RadGrid1.MasterTableView.Columns.Add(boundColumn2);
    boundColumn2.DataField = "shipcity";
    boundColumn2.HeaderText = "shipcity";   
 
    string templateColumnName = "employeeid";
    GridTemplateColumn templateColumn = new GridTemplateColumn();
    templateColumn.ItemTemplate = new MyTemplate(templateColumnName);
    templateColumn.HeaderText = templateColumnName;
    templateColumn.DataField = "employeeid";
    templateColumn.HeaderText = "EmployeeID";
    templateColumn.UniqueName = "TemplateColumn";
    RadGrid1.MasterTableView.Columns.Add(templateColumn);
    PlaceHolder1.Controls.Add(RadGrid1);
    
}
public class MyTemplate : ITemplate
{
    private string colname;
    protected Label lControl;      
 
    public MyTemplate(string cName)
    {
        colname = cName;
            
    }
    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;
        l.Text = ((DataRowView)container.DataItem)[colname].ToString() + "<br />";
    }
}

Thanks,
Princy
0
cha
Top achievements
Rank 1
answered on 04 Dec 2013, 06:54 PM
If the template column has asp.net tables in headertemplate or itemtemplate then those values do not get exported.
Any idea how we can fix this ?
0
Princy
Top achievements
Rank 2
answered on 05 Dec 2013, 06:16 AM
Hi,

Please try setting ExportOnlyData="false" on the Export button click. Let me know if any concerns.

Thanks,
Princy
Tags
Grid
Asked by
oscarcuestameza
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Nicolaï
Top achievements
Rank 2
Princy
Top achievements
Rank 2
cha
Top achievements
Rank 1
Share this question
or