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

Missing Columns collection in InfrastructureExporting when colums are added programmatically

2 Answers 367 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Lars-Marcus
Top achievements
Rank 1
Lars-Marcus asked on 24 Feb 2016, 02:03 PM

We would like to set the width of columns in xlsx export from a RadGrid. I do this by using e.ExportStructure.Tables[0].Columns in the InfrastructureExporting event handler. If columns are declaratively set in the aspx file when this works fine.

If, however, the columns of the RadGrid are added by setting AutoGenerateColumns="true" or manually in Page_Load, then the Columns collection is empty. Why is this, and are there some other way to set the width of the columns in the xlsx file?

ASPX:

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="true" OnNeedDataSource="RadGrid1_NeedDataSource" OnInfrastructureExporting="RadGrid1_InfrastructureExporting">
   <ExportSettings Excel-Format="Xlsx" />
</telerik:RadGrid>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" />

.cs:

public class Person
{
   public string ID { get; set; }
   public string Name { get; set; }
   public DateTime? Date { get; set; }
}

protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
   RadGrid1.DataSource = new List<Person> {
     new Person { ID = "1", Name = "Adam", Date = new DateTime(1973, 4, 12) },
     new Person { ID = "2", Name = "Bertil", Date = null },
     new Person { ID = "3", Name = "Ceasar", Date = new DateTime(1973, 10, 27) },
   };
}

protected void Button1_Click(object sender, EventArgs e)
{
   RadGrid1.MasterTableView.ExportToExcel();
}

protected void RadGrid1_InfrastructureExporting(object sender, Telerik.Web.UI.GridInfrastructureExportingEventArgs e)
{
  e.ExportStructure.ColumnWidthUnit = Telerik.Web.UI.ExportInfrastructure.ExportUnitType.FormatDefault;

  // ERROR: The Columns collection is empty if the columns of radgrid are auto-generated or dynamically added in code behind
  foreach (var col in e.ExportStructure.Tables[0].Columns)
  {
    col.Width = 20;
  }
}

 

/Patrik

2 Answers, 1 is accepted

Sort by
0
Accepted
Kostadin
Telerik team
answered on 29 Feb 2016, 10:40 AM
Hello Egil,

Note that the columns collection is always empty if you are not specifying a width of the columns. Nevertheless, you can still apply the width to the Excel columns by traversing the grid columns collection. Please check out the following code snippet.
protected void RadGrid1_InfrastructureExporting(object sender, Telerik.Web.UI.GridInfrastructureExportingEventArgs e)
{
    e.ExportStructure.ColumnWidthUnit = Telerik.Web.UI.ExportInfrastructure.ExportUnitType.FormatDefault;
 
    for (int i = 0; i < RadGrid1.MasterTableView.AutoGeneratedColumns.Length; i++)
    {
        e.ExportStructure.Tables[0].Columns[i + 1].Width = 20;
    }
}


Regards,
Kostadin
Telerik
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 Feedback Portal and vote to affect the priority of the items
0
Lars-Marcus
Top achievements
Rank 1
answered on 09 Mar 2016, 01:25 PM
Thanks. I did not realize that I could add items to the ColumnCollection. This is perfect.
Tags
Grid
Asked by
Lars-Marcus
Top achievements
Rank 1
Answers by
Kostadin
Telerik team
Lars-Marcus
Top achievements
Rank 1
Share this question
or