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

Extra columns during PDF export

12 Answers 211 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Scott
Top achievements
Rank 1
Scott asked on 26 Oct 2012, 10:35 PM
PDF export of X columns is producing another extra X blank columns.  This must be some really simple error, but I just can't find it.  Here's the code:
<asp:Button ID="ExportToPDF" runat="server" Text="ExportToPDF"
    OnClick="ExportToPDF_Click"/>
<telerik:RadGrid ID="RadGrid2" runat="server" GridLines="Vertical" AutoGenerateColumns="false" Width="800px">
<ExportSettings ExportOnlyData="true"></ExportSettings>
</telerik:RadGrid>
    public partial class gridtest : System.Web.UI.UserControl
    {
        int iNumCols = 5;
 
        protected void Page_Init(object sender, EventArgs e)
        {
            CreateRadGrid2();
        }
 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                FillRadGrid2();
        }
 
        internal void CreateRadGrid2()
        {
            for (int iCol = 0; iCol < iNumCols; iCol++)
            {
                GridBoundColumn col = new GridBoundColumn();
                col.HeaderText = "Col"+iCol.ToString();
                col.DataField = "Col"+iCol.ToString();
                RadGrid2.MasterTableView.Columns.Add(col);
            }
        }
 
        internal void FillRadGrid2()
        {
            // create an empty data table, matching the bound columns of the grid
            DataTable dt = new DataTable();
            for (int iCol = 0; iCol < iNumCols; iCol++)
                dt.Columns.Add("Col"+iCol.ToString());
 
            // fill in the data for the rows
            for (int iRow = 0; iRow < 5; iRow++)
            {
                DataRow dr = dt.NewRow();
                for (int iCol = 0; iCol < iNumCols; iCol++)
                    dr["Col" + iCol.ToString()] = "R" + iRow.ToString() + "C" + iCol.ToString();
                dt.Rows.Add(dr);
            }
            RadGrid2.DataSource = dt;
            RadGrid2.DataBind();
        }
 
        protected void ExportToPDF_Click(object sender, EventArgs e)
        {
            this.RadGrid2.MasterTableView.ExportToPdf();
        }
}

The resulting PDF is attached.

12 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 28 Oct 2012, 06:24 PM
Hello,

<div>
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
        <asp:Button ID="ExportToPDF" runat="server" Text="ExportToPDF" OnClick="ExportToPDF_Click" />
        <telerik:RadGrid ID="RadGrid2" runat="server" GridLines="Vertical" AutoGenerateColumns="false"
            Width="800px" onprerender="RadGrid2_PreRender">
            <ExportSettings ExportOnlyData="true">
            </ExportSettings>
        </telerik:RadGrid>
    </div>

public partial class zdynamic : System.Web.UI.Page
{
    int iNumCols = 5;
 
    protected void Page_Init(object sender, EventArgs e)
    {
        CreateRadGrid2();
    }
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillRadGrid2();
        }
    }
 
    internal void CreateRadGrid2()
    {  
        for (int iCol = 0; iCol < iNumCols; iCol++)
        {
            GridBoundColumn col = new GridBoundColumn();
            col.HeaderText = "Col" + iCol.ToString();
            col.DataField = "Col" + iCol.ToString();
            RadGrid2.MasterTableView.Columns.Add(col);
        }
    }
 
    internal void FillRadGrid2()
    {
        // create an empty data table, matching the bound columns of the grid
        DataTable dt = new DataTable();
        for (int iCol = 0; iCol < iNumCols; iCol++)
            dt.Columns.Add("Col" + iCol.ToString());
 
        // fill in the data for the rows
        for (int iRow = 0; iRow < 5; iRow++)
        {
            DataRow dr = dt.NewRow();
            for (int iCol = 0; iCol < iNumCols; iCol++)
                dr["Col" + iCol.ToString()] = "R" + iRow.ToString() + "C" + iCol.ToString();
            dt.Rows.Add(dr);
        }
        RadGrid2.DataSource = dt;
        RadGrid2.DataBind();
    }
 
    protected void ExportToPDF_Click(object sender, EventArgs e)
    {
        this.RadGrid2.MasterTableView.ExportToPdf();
    }
 
    protected void RadGrid2_PreRender(object sender, EventArgs e)
    {
        int count = 0;
        foreach (GridColumn column in RadGrid2.Columns)
        {
            count++;
            if (count > iNumCols )
            {
                column.Visible = false;
            }
        }
    }
 
}


Thanks,
Jayesh Goyani
0
Kostadin
Telerik team
answered on 29 Oct 2012, 03:08 PM
Hello Scott,

I suggest you to modify your code as follow:
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CreateRadGrid2();
            FillRadGrid2();
        }
    }

You are creating the table again when you click the export button, because you call CreateRadGrid2() method in Page_Init.

Regards,
Kostadin
the Telerik team
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 their blog feed now.
0
Scott
Top achievements
Rank 1
answered on 29 Oct 2012, 08:18 PM
Thank Jayesh.  This is a kludge (shouldn't have to do this), but it works.  To make it slightly more general, I am changing it to:
protected void RadGrid2_PreRender(object sender, EventArgs e)
{
    RadGrid thisGrid = (RadGrid)sender;
 
    foreach(GridColumn column in thisGrid.Columns)
        if(column.HeaderText.Equals(""))
            column.Visible = false;
}
since I am setting the HeaderText on all of the columns I want to keep.

Any idea why I am getting these extra columns?
0
Scott
Top achievements
Rank 1
answered on 29 Oct 2012, 08:22 PM
Kostadin, thank you for your suggestion.  I actually already tried that, and it did prevent the extra columns (although it removed the HeaderText somehow).  Unfortunately, this is a simplified version of a more complex grid involving Templated columns (the code was very long so I simplified it), and my understanding is that Dynamic Templated Columns have to be added in the Page_Init, not the Page_Load.
0
Kostadin
Telerik team
answered on 01 Nov 2012, 03:18 PM
Hello Scott,

If you are using more complex grid than I suggest you to use Advanced DataBinding instead Simple DataBinding. The advantage of the advanced data binding is that the RadGrid fires the NeedDataSource event each time it needs to be bound to a data source.
Also more information on defining the RadGrid structure programmatically could be found here:
http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html

Regards,
Kostadin
the Telerik team
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 their blog feed now.
0
Scott
Top achievements
Rank 1
answered on 01 Nov 2012, 08:46 PM
Kostadin, thanks for the suggestion.  Unfortunately, using Advanced Binding did not resolve the issue; I still end up with twice as many columns in the PDF. 

Were you able to replicate the issue?  If not, then it could be some other issue in how I am building the page.
-Scott
0
Kostadin
Telerik team
answered on 06 Nov 2012, 03:26 PM
Hi Scott,

When creating RadGrid on Page_Load event, the columns or detail tables should be added to the corresponding collection first and then values for the properties of this instance should be set. I noticed that in your project you do it in reverse order. Try modify your project as it is shown below:
internal void CreateRadGrid2()
        {
            for (int iCol = 0; iCol < iNumCols; iCol++)
            {
                GridBoundColumn col = new GridBoundColumn();
                RadGrid2.MasterTableView.Columns.Add(col);
                col.HeaderText = "Col"+iCol.ToString();
                col.DataField = "Col"+iCol.ToString();
                 
            }
        }


Kind regards,
Kostadin
the Telerik team
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 their blog feed now.
0
Scott
Top achievements
Rank 1
answered on 06 Nov 2012, 05:38 PM
Kostadin, thanks for the suggestion. Unfortunately, adding the columns before setting their HeaderText and DataField did not resolve the issue; I still end up with twice as many columns in the PDF, although now the extra columns have HeaderText (they were just blank before).

Were you able to replicate the issue? If not, then it could be some other issue in how I am building the page.
-Scott
0
Scott
Top achievements
Rank 1
answered on 06 Nov 2012, 08:29 PM
OK, so the fix turned out to be fairly simple.  I needed to check for "If (!IsPostBack)" in the Page_Init.  This, combined with the suggestion by Kostadin to add columns before setting their properties seems to have done the trick.  The final code is:
<asp:Button ID="ExportToPDF" runat="server" Text="ExportToPDF" OnClick="ExportToPDF_Click"/>
 <telerik:RadGrid ID="RadGrid2" runat="server" GridLines="Vertical" AutoGenerateColumns="false" Width="800px">
       <ExportSettings ExportOnlyData="true"></ExportSettings>
</telerik:RadGrid>
public partial class gridtest : System.Web.UI.UserControl
{
    int iNumCols = 5;
 
    protected void Page_Init(object sender, EventArgs e)
    {
        if (!IsPostBack)
            CreateRadGrid2();
    }
  
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            FillRadGrid2();
    }
 
    internal void CreateRadGrid2()
    {
        for (int iCol = 0; iCol < iNumCols; iCol++)
        {
            GridBoundColumn col = new GridBoundColumn();
            RadGrid2.MasterTableView.Columns.Add(col);
            col.HeaderText = "Col"+iCol.ToString();
            col.DataField = "Col"+iCol.ToString();
        }
    }
 
    internal void FillRadGrid2()
    {
        // create an empty data table, matching the bound columns of the grid
        DataTable dt = new DataTable();
        for (int iCol = 0; iCol < iNumCols; iCol++)
            dt.Columns.Add("Col"+iCol.ToString());
 
        // fill in the data for the rows
        for (int iRow = 0; iRow < 5; iRow++)
        {
            DataRow dr = dt.NewRow();
            for (int iCol = 0; iCol < iNumCols; iCol++)
                dr["Col" + iCol.ToString()] = "R" + iRow.ToString() + "C" + iCol.ToString();
            dt.Rows.Add(dr);
        }
        RadGrid2.DataSource = dt;
        RadGrid2.DataBind();
    }
  
    protected void ExportToPDF_Click(object sender, EventArgs e)
    {
        this.RadGrid2.MasterTableView.ExportToPdf();
     }
}

-Scott
0
Kostadin
Telerik team
answered on 09 Nov 2012, 10:40 AM
Hello Scott,

I am glad you have resolved the issue. My suggestion was to create the grid in Page_Load event handler.
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        CreateRadGrid2();
        FillRadGrid2();
    }
}


Regards,
Kostadin
the Telerik team
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 their blog feed now.
0
Scott
Top achievements
Rank 1
answered on 09 Nov 2012, 05:00 PM
Yes, and that works, except that the next step I want to do is to use Templated Columns, which I understand requires the columns to be created in the Page_Init method.
-Scott
0
Accepted
Kostadin
Telerik team
answered on 14 Nov 2012, 01:33 PM
Hi Scott,

I suggest you to take a look at this help article where is described how to create the grid dynamically. Note that if you want to use template columns you have to create the whole grid on Page_Init event handler. 

All the best,
Kostadin
the Telerik team
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 their blog feed now.
Tags
Grid
Asked by
Scott
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Kostadin
Telerik team
Scott
Top achievements
Rank 1
Share this question
or