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

[Solved] RadGrid ExportToExcel/Word

3 Answers 164 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Pat Lindley
Top achievements
Rank 1
Pat Lindley asked on 24 Jun 2009, 09:09 PM
I've noticed that when I have a radgrid with "AutoGenerateColumns='false'" and then set the export property "RadGrid1.ExportSettings.IgnorePaging = false" no data from the grid is exported to excel.  If I remove the AutoGenerateColumns property/attribute, it works fine.

I've applied the AutoGenerateColumns attribute to both the RadGrid and the MasterTableView (not at the same time), and even set it as a property in the code behind, but nothing is working. 

Here's the grid as I currently have it:

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true">
<MasterTableView >
</MasterTableView>
</telerik:RadGrid>

And Here's the codebehind that calls the ExportToExcell method:

private void btnExcel_Click(object sender, EventArgs e)
{
ConfigureReport();
        RadGrid1.MasterTableView.ExportToExcel();
}

private void ConfigureReport()
{
        RadGrid1.ExportSettings.FileName = "RadGridExportToExcel";
        RadGrid1.ExportSettings.IgnorePaging = this.cbxPaging.Checked;
        RadGrid1.ExportSettings.ExportOnlyData = this.cbxFormat.Checked;
        RadGrid1.ExportSettings.OpenInNewWindow = true;
}


Any ideas?

Thanks,
Pat Lindley

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 25 Jun 2009, 05:13 AM
Hi Pat Lindley,

The export functionality must be working well as expected. In the above given aspx code I can see that you have set the AutoGenerateColumns property to false and also there is no manually added columns in the Grid declaration. In that case the Grid won't be having any data. I am not sure whether you are adding any columns to the Grid in the code behind. If you have not added any columns to the Grid in the code behind try adding it in the aspx and see whether data is getting exported to excel.

Shinu
0
Pat Lindley
Top achievements
Rank 1
answered on 25 Jun 2009, 02:26 PM
Sorry, I failed to mention that yes I do have a method that creates the columns dynamically during runtime based on user input.  The situation occurs when the user selects to not ignore paging, or sets IgnorePaging = false.  This along with the AutoGenerateColumns=false prevents data from being exported.  The user can select to ingore paging and it works fine.  I'm testing this on my local machine with windows XP pro SP3 and Office 2007.

Here's the code I'm using to create the columns:

  

public void CreateColumns()
{
this.RadGrid1.MasterTableView.DataMember = "Enrollments";
this.RadGrid1.MasterTableView.DataKeyNames = new string[] {"EnrollmentID"};
this.RadGrid1.MasterTableView.Columns.Clear();
GridBoundColumn IDboundColumn = new GridBoundColumn
    {
        DataField = "EnrollmentID",
        HeaderText = "ID",
        UniqueName = "ID",
        SortExpression = "EnrollmentID",
        Visible = false
    };
this.RadGrid1.MasterTableView.Columns.Add(IDboundColumn);

Columns tmpCol;
for(int j = 0; j < this.UserProcess.ReportColumnList.Count; j++)
{
tmpCol = (Columns)this.UserProcess.ReportColumnList[j];
if(tmpCol.Selected)
{
GridBoundColumn boundColumn = new GridBoundColumn
         {
      DataField = tmpCol.Name,
      HeaderText = tmpCol.LocalizationName,
      UniqueName = tmpCol.Name,
      SortExpression = tmpCol.Name,
      HeaderButtonType = GridHeaderButtonType.TextButton
         };
this.RadGrid1.MasterTableView.Columns.Add(boundColumn);
}
}
}

 
0
Daniel
Telerik team
answered on 01 Jul 2009, 01:18 PM
Hello Pat,

Could you please try the following approach?
protected void Page_Load(object o, EventArgs e) 
    if (!IsPostBack) 
        CreateColumns(); 
public void CreateColumns() 
    this.RadGrid1.MasterTableView.DataMember = "Enrollments"
    this.RadGrid1.MasterTableView.DataKeyNames = new string[] { "EnrollmentID" }; 
 
    GridBoundColumn IDboundColumn = new GridBoundColumn(); 
    this.RadGrid1.MasterTableView.Columns.Add(IDboundColumn); 
    IDboundColumn.DataField = "EnrollmentID"
    IDboundColumn.HeaderText = "ID"
    IDboundColumn.UniqueName = "ID"
    IDboundColumn.SortExpression = "EnrollmentID"
    IDboundColumn.Visible = false
 
    Columns tmpCol; 
    for (int j = 0; j < this.UserProcess.ReportColumnList.Count; j++) 
    { 
        tmpCol = (Columns)this.UserProcess.ReportColumnList[j]; 
        if (tmpCol.Selected) 
        { 
            GridBoundColumn boundColumn = new GridBoundColumn(); 
            this.RadGrid1.MasterTableView.Columns.Add(boundColumn); 
            boundColumn.DataField = tmpCol.Name; 
            boundColumn.HeaderText = tmpCol.LocalizationName; 
            boundColumn.UniqueName = tmpCol.Name; 
            boundColumn.SortExpression = tmpCol.Name; 
            boundColumn.HeaderButtonType = GridHeaderButtonType.TextButton; 
        } 
    } 

To define the structure of a RadGrid control that is declared in the ASPX page, use the Page_Load event handler. Columns and detail table should be added to the corresponding collection first, before the values for their properties are set. This is important because no ViewState is managed for the object before it has been added to the corresponding collection.
Be sure to check that IsPostBack is false. Otherwise, you will end up adding the same objects to the RadGrid multiple times.

Regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
Pat Lindley
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Pat Lindley
Top achievements
Rank 1
Daniel
Telerik team
Share this question
or