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

Exporting programtically created template columns

1 Answer 27 Views
Grid
This is a migrated thread and some comments may be shown as answers.
cha
Top achievements
Rank 1
cha asked on 04 Dec 2013, 05:15 PM
I have  a radgrid with programtically created template columns. There are no fancy controls in the template columns. Just Links and Table.
When I download as excel it doesnt download any of the template column data. Attached below is the source code sample. I've also attached image snapshot of html rendered and excel downloaded.  Creating template columns dynamically is critical for me as I'll not know the number of columns until I run some database queries to get the data.
Appreciate your help.


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ScaleAnalysisTemplateTest.aspx.cs" Inherits="DSSAnalytics.DashboardWidgets.ScaleAnalysisTool.ScaleAnalysisTemplateTest" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="MuniWebScriptMgr" runat="server" EnablePartialRendering="true"
        AsyncPostBackTimeout="36000">
        <Scripts>
            <asp:ScriptReference Path="~/scripts/jquery-1.3.2.js" />
        </Scripts>
    </telerik:RadScriptManager>
 
 
    <asp:ImageButton runat="server" ID="IMG_Excel" OnClick="DownloadAsExcel" CssClass="ExcelBtnStyle"
        ImageUrl="../../images/excel.png" ToolTip="Export to Excel" />
    <div id="gridCtrl" runat="server">
     
    </div>
    </form>
</body>
</html>


    RadGrid r;
 
    protected void Page_Init(object sender, EventArgs e)
    {
        BuildRadGrid();
    }
 
 
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
 
    private void BuildRadGrid()
    {
        r = new RadGrid();
        r.ID = "ScaleAnalysisSummaryByRow";
        r.GridLines = GridLines.None;
        r.EnableEmbeddedSkins = false;
        r.MasterTableView.AutoGenerateColumns = false;
        r.MasterTableView.TableLayout = GridTableLayout.Fixed;
        r.AllowSorting = true;
        r.MasterTableView.AllowNaturalSort = true;
        r.ClientSettings.Scrolling.SaveScrollPosition = false;
        r.EnableViewState = false;
        r.Height = Unit.Pixel(450);
        r.Width = Unit.Pixel(1200);
        r.MasterTableView.Width = Unit.Pixel(900);
 
        GridBoundColumn boundColumn1 = new GridBoundColumn();
        boundColumn1.HeaderText = "Name";
        boundColumn1.DataField = "name";
        boundColumn1.UniqueName = "name";
        boundColumn1.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
        boundColumn1.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
        boundColumn1.HeaderStyle.VerticalAlign = VerticalAlign.Bottom;
        boundColumn1.HeaderStyle.Width = Unit.Pixel(150);
        boundColumn1.HeaderStyle.Font.Bold = true;
 
        r.Columns.Add(boundColumn1);
 
        GridTemplateColumn boundColumn2 = new GridTemplateColumn();
        boundColumn2.HeaderText = "Series";
        boundColumn2.ItemTemplate = new ScaleAnalysisRowSeriesNameTemplate();
        boundColumn2.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
        boundColumn2.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
        boundColumn2.HeaderStyle.VerticalAlign = VerticalAlign.Bottom;
        boundColumn2.HeaderStyle.Width = Unit.Pixel(130);
        boundColumn2.HeaderStyle.Font.Bold = true;
        r.Columns.Add(boundColumn2);
 
        GridBoundColumn boundColumn3 = new GridBoundColumn();
        boundColumn3.HeaderText = "Amount";
        boundColumn3.DataField = "amt";
        boundColumn3.UniqueName = "amt";
        boundColumn3.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
        boundColumn3.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
        boundColumn3.HeaderStyle.VerticalAlign = VerticalAlign.Bottom;
        boundColumn3.HeaderStyle.Width = Unit.Pixel(60);
        boundColumn3.HeaderStyle.Font.Bold = true;
        boundColumn3.DataFormatString = "{0:N0}";
        r.Columns.Add(boundColumn3);
 
 
        int year = 2010;
        string col1 =  "col1_d1";
        string col2 =  "col1_d2";
        string col3 =  "col1_d3";
        string col4 =  "col1_d4";
        GridTemplateColumn templateColumn = new GridTemplateColumn();
        templateColumn.HeaderTemplate = new ScaleAnalysisRowHeaderTemplate(year);
        templateColumn.ItemTemplate = new ScaleAnalysisRowItemTemplate(col1 , col2, col3 , col4);
        r.Columns.Add(templateColumn);
                               
        year = 2011;
        col1 =  "col2_d1";
        col2 =  "col2_d2";
        col3 =  "col2_d3";
        col4 =  "col2_d4";
        GridTemplateColumn templateColumn2 = new GridTemplateColumn();
        templateColumn2.HeaderTemplate = new ScaleAnalysisRowHeaderTemplate(year);
        templateColumn2.ItemTemplate = new ScaleAnalysisRowItemTemplate(col1, col2, col3, col4);
        r.Columns.Add(templateColumn2);
 
        r.ClientSettings.Scrolling.AllowScroll = true;
        r.ClientSettings.Scrolling.UseStaticHeaders = true;
        r.ClientSettings.Selecting.AllowRowSelect = true;
 
        r.NeedDataSource += new GridNeedDataSourceEventHandler(Grid_NeedDataSource);
 
        gridCtrl.Controls.Add(r);
 
    }
 
    protected void Grid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
 
        DataTable dt = new DataTable();
        dt.Columns.Add("name", typeof(String));
        dt.Columns.Add("series", typeof(String));
        dt.Columns.Add("amt", typeof(Decimal));
 
        dt.Columns.Add("col1_d1", typeof(Decimal));
        dt.Columns.Add("col1_d2", typeof(Decimal));
        dt.Columns.Add("col1_d3", typeof(Decimal));
        dt.Columns.Add("col1_d4", typeof(Decimal));
 
        dt.Columns.Add("col2_d1", typeof(Decimal));
        dt.Columns.Add("col2_d2", typeof(Decimal));
        dt.Columns.Add("col2_d3", typeof(Decimal));
        dt.Columns.Add("col2_d4", typeof(Decimal));
 
        DataRow dr = dt.NewRow();
        dr["name"] = "John Smith";
        dr["series"] = "S1";
        dr["amt"] = 200.98;
        dr["col1_d1"] = 120;
        dr["col1_d2"] = 11;
        dr["col1_d3"] = 12090;
        dr["col1_d4"] = 1221;
 
        dr["col2_d1"] = 320;
        dr["col2_d2"] = 110;
        dr["col2_d3"] = 1190;
        dr["col2_d4"] = 121;
 
 
        dt.Rows.Add(dr);
 
        dr = dt.NewRow();
        dr["name"] = "Mary Jones";
        dr["series"] = "S2";
        dr["amt"] = 200.98;
        dr["col1_d1"] = 120;
        dr["col1_d2"] = 11;
        dr["col1_d3"] = 12090;
        dr["col1_d4"] = 1221;
 
        dr["col2_d1"] = 20;
        dr["col2_d2"] = 10;
        dr["col2_d3"] = 190;
        dr["col2_d4"] = 21;
 
        dt.Rows.Add(dr);
 
 
        dr = dt.NewRow();
        dr["name"] = "Test User";
        dr["series"] = "S3";
        dr["amt"] = 200.98;
        dr["col1_d1"] = 120;
        dr["col1_d2"] = 11;
        dr["col1_d3"] = 12090;
        dr["col1_d4"] = 1221;
 
        dr["col2_d1"] = 200;
        dr["col2_d2"] = 109;
        dr["col2_d3"] = 1990;
        dr["col2_d4"] = 218;
 
        dt.Rows.Add(dr);
                     
        RadGrid grid = (RadGrid)source;
        grid.DataSource = dt;
    }
 
 
    protected void DownloadAsExcel(object sender, EventArgs e)
    {
        try
        {
            r.ExportSettings.IgnorePaging = true;
            r.ExportSettings.OpenInNewWindow = true;
            r.ExportSettings.ExportOnlyData = true;
            r.ExportSettings.FileName = "ExcelSample";
            r.MasterTableView.ExportToExcel();
        }
        catch (Exception ex)
        {
            throw ex;
 
        }
    }
 
}
 
 
 
internal class ScaleAnalysisRowSeriesNameTemplate : ITemplate
{
    protected LiteralControl securityWindow;
 
    public ScaleAnalysisRowSeriesNameTemplate()
    {
 
    }
 
    public void InstantiateIn(System.Web.UI.Control container)
    {
        Table table = new Table();
        table.CellSpacing = 0;
        table.CellPadding = 0;
        table.Width = Unit.Percentage(100);
        table.BorderStyle = BorderStyle.None;
 
        TableRow tr1 = new TableRow();
        TableCell td1 = new TableCell();
        td1.BorderStyle = BorderStyle.None;
        td1.BorderWidth = Unit.Pixel(0);
 
        securityWindow = new LiteralControl();
        securityWindow.DataBinding += new EventHandler(lControl_Series_DataBinding);
 
 
        td1.Controls.Add(securityWindow);
        td1.HorizontalAlign = HorizontalAlign.Left;
        td1.VerticalAlign = VerticalAlign.Middle;
 
        td1.Controls.Add(securityWindow);
        tr1.Cells.Add(td1);
 
        table.Rows.Add(tr1);
        container.Controls.Add(table);
 
    }
 
    public void lControl_Series_DataBinding(object sender, EventArgs e)
    {
 
        LiteralControl l = (LiteralControl)sender;
        GridDataItem container = (GridDataItem)l.NamingContainer;
        string securityWinLink = "openwin('" + ((DataRowView)container.DataItem)["series"].ToString() + "')";
        l.Text = "<a onclick=" + securityWinLink + " title='Click to Open series details'><b><u>" + ((DataRowView)container.DataItem)["series"].ToString() + "</u></b></a>";
 
    }
 
}
 
 
internal class ScaleAnalysisRowHeaderTemplate : ITemplate
{
 
    private int _year;
 
 
    public ScaleAnalysisRowHeaderTemplate(int year)
    {
        _year = year;
 
    }
 
    public void InstantiateIn(System.Web.UI.Control container)
    {
        Table table = new Table();
        table.CellSpacing = 0;
        table.CellPadding = 0;
        table.Width = Unit.Percentage(100);
        table.BorderStyle = BorderStyle.None;
 
        TableRow tr0 = new TableRow();
        TableRow tr1 = new TableRow();
        TableRow tr2 = new TableRow();
 
        int colspan = 2;
 
 
        TableCell td0 = new TableCell();
        TableCell td1 = new TableCell();
        TableCell td2 = new TableCell();
        TableCell td3 = new TableCell();
        TableCell td4 = new TableCell();
 
        td0.HorizontalAlign = HorizontalAlign.Center;
        td0.Text = _year.ToString();
        td0.Font.Bold = true;
        tr0.Cells.Add(td0);
        table.Rows.Add(tr0);
 
        colspan = 2;
        td1.Text = "Data1";
        td2.Text = "Data2";
        td3.Text = "Data3";
        td4.Text = "Data4";
        td1.HorizontalAlign = HorizontalAlign.Center;
        td2.HorizontalAlign = HorizontalAlign.Center;
        td3.HorizontalAlign = HorizontalAlign.Center;
        td4.HorizontalAlign = HorizontalAlign.Center;
        td1.Font.Bold = true;
        td2.Font.Bold = true;
        td3.Font.Bold = true;
        td4.Font.Bold = true;
        td1.Width = Unit.Percentage(50);
        td2.Width = Unit.Percentage(50);
        tr1.Cells.Add(td1);
        tr1.Cells.Add(td2);
        tr2.Cells.Add(td3);
        tr2.Cells.Add(td4);
        table.Rows.Add(tr1);
        table.Rows.Add(tr2);
        td0.ColumnSpan = colspan;
 
        container.Controls.Add(table);
 
    }
}
 
 
 
internal class ScaleAnalysisRowItemTemplate : ITemplate
{
    string _col1;
    string _col2;
    string _col3;
    string _col4;
    string _view;
    protected LiteralControl lControl1;
    protected LiteralControl lControl2;
    protected LiteralControl lControl3;
    protected LiteralControl lControl4;
    int _colcounter;
 
    public ScaleAnalysisRowItemTemplate(string col1, string col2, string col3, string col4)
    {
        _col1 = col1;
        _col2 = col2;
        _col3 = col3;
        _col4 = col4;
    }
 
    public void InstantiateIn(System.Web.UI.Control container)
    {
        Table table = new Table();
        table.CellSpacing = 0;
        table.CellPadding = 0;
        table.Width = Unit.Percentage(100);
        table.BorderStyle = BorderStyle.None;
 
        TableRow tr1 = new TableRow();
        TableRow tr2 = new TableRow();
 
        TableCell td1 = new TableCell();
        TableCell td2 = new TableCell();
        TableCell td3 = new TableCell();
        TableCell td4 = new TableCell();
 
        lControl1 = new LiteralControl();
        lControl1.ID = "lControl1" + _col1 + _colcounter.ToString();
        lControl1.DataBinding += new EventHandler(lControl_col1_DataBinding);
 
        lControl2 = new LiteralControl();
        lControl2.ID = "lControl2" + _col2 + _colcounter.ToString();
        lControl2.DataBinding += new EventHandler(lControl_col2_DataBinding);
 
        lControl3 = new LiteralControl();
        lControl3.ID = "lControl3" + _col3 + _colcounter.ToString();
        lControl3.DataBinding += new EventHandler(lControl_col3_DataBinding);
 
        lControl4 = new LiteralControl();
        lControl4.ID = "lControl4" + _col4 + _colcounter.ToString(); ;
        lControl4.DataBinding += new EventHandler(lControl_col4_DataBinding);
 
        td1.Controls.Add(lControl1);
        td2.Controls.Add(lControl2);
        td3.Controls.Add(lControl3);
        td4.Controls.Add(lControl4);
 
        td1.HorizontalAlign = HorizontalAlign.Center;
        td2.HorizontalAlign = HorizontalAlign.Center;
        td3.HorizontalAlign = HorizontalAlign.Center;
        td4.HorizontalAlign = HorizontalAlign.Center;
 
        td1.BorderStyle = BorderStyle.None;
        td1.BorderWidth = Unit.Pixel(0);
 
        td2.BorderStyle = BorderStyle.None;
        td2.BorderWidth = Unit.Pixel(0);
 
        td3.BorderStyle = BorderStyle.None;
        td3.BorderWidth = Unit.Pixel(0);
 
        td4.BorderStyle = BorderStyle.None;
        td4.BorderWidth = Unit.Pixel(0);
 
        td1.Width = Unit.Percentage(50);
        td2.Width = Unit.Percentage(50);
        tr1.Cells.Add(td1);
        tr1.Cells.Add(td2);
        tr2.Cells.Add(td3);
        tr2.Cells.Add(td4);
        table.Rows.Add(tr1);
        table.Rows.Add(tr2);
 
 
        _colcounter++;
 
        container.Controls.Add(table);
 
    }
 
 
    public void lControl_col1_DataBinding(object sender, EventArgs e)
    {
        LiteralControl l = (LiteralControl)sender;
        GridDataItem container = (GridDataItem)l.NamingContainer;
 
        l.Text = String.Format("{0:N0}", ((DataRowView)container.DataItem)[_col1]);
 
    }
 
    public void lControl_col2_DataBinding(object sender, EventArgs e)
    {
        LiteralControl l = (LiteralControl)sender;
        GridDataItem container = (GridDataItem)l.NamingContainer;
        l.Text = String.Format("{0:N2}", ((DataRowView)container.DataItem)[_col2]);
    }
 
 
    public void lControl_col3_DataBinding(object sender, EventArgs e)
    {
        LiteralControl l = (LiteralControl)sender;
        GridDataItem container = (GridDataItem)l.NamingContainer;
        l.Text = String.Format("{0:N2}", ((DataRowView)container.DataItem)[_col3]);
    }
 
 
    public void lControl_col4_DataBinding(object sender, EventArgs e)
    {
        LiteralControl l = (LiteralControl)sender;
        GridDataItem container = (GridDataItem)l.NamingContainer;
        l.Text = String.Format("{0:N0}", ((DataRowView)container.DataItem)[_col4]);
    }
 
 
}

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 05 Dec 2013, 12:01 PM
Hi,

For exporting with tables and links you can try setting the ExportOnlyData = false.

C#:
r.ExportSettings.ExportOnlyData = false;

Thanks,
Shinu
Tags
Grid
Asked by
cha
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or