Missing Columns collection in InfrastructureExporting when colums are added programmatically

3 posts, 1 answers
  1. Lars-Marcus
    Lars-Marcus avatar
    2 posts
    Member since:
    Jul 2013

    Posted 24 Feb Link to this post

    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. Answer
    Kostadin
    Admin
    Kostadin avatar
    1708 posts

    Posted 29 Feb Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Lars-Marcus
    Lars-Marcus avatar
    2 posts
    Member since:
    Jul 2013

    Posted 09 Mar in reply to Kostadin Link to this post

    Thanks. I did not realize that I could add items to the ColumnCollection. This is perfect.
Back to Top