Export to Excel, via code behind generated grid.. wiring up events...

9 posts, 1 answers
  1. Wired_Nerve
    Wired_Nerve avatar
    163 posts
    Member since:
    Oct 2007

    Posted 12 Jun 2012 Link to this post

    We are using a asp:PlaceHolder control that we inject (add) a code generated grid to, which then is used to output the excel report.
    I have a method that creates the grid, and wires up several events...
    I am trying to figure out how to wire up the event ExportCellFormatting...

    protected void grid_ExportCellFormatting(object source, ExcelExportCellFormattingEventArgs e)
            {
                // Set all cells to string to include leading zeros.
                e.Cell.Style["mso-number-format"] = @"\@";
            }

    but it just does not like what i have done so far.. 


    The error I get is 
    Error	2	No overload for 'grid_ExportCellFormatting' matches delegate 'System.EventHandler<Telerik.Web.UI.ExportCellFormattingEventArgs>'	C:\TFS 2008\TIPWeb Scrum\Dev\TIPWebITApp\TIPWebIT\TagManagement\Tags.aspx.cs	650	46	TIPWebIT
    protected void CreateGrid()
           {
               using (RadGrid grid = new RadGrid())
               {
                   grid.NeedDataSource += new GridNeedDataSourceEventHandler(grid_NeedDataSource);
                   grid.ExcelMLExportRowCreated += new GridExcelMLExportRowCreatedEventHandler(grid_ExcelMLExportRowCreated);
                   grid.ExcelMLExportStylesCreated += new GridExcelMLExportStylesCreatedEventHandler(grid_ExcelMLExportStylesCreated);
     // HERE IS MY PROBLEM -------- Remember I do not have the grid on the actual aspx page, it is inserted into the place holder tag...
                   grid.ExportCellFormatting += new EventHandler<ExportCellFormattingEventArgs>(grid_ExportCellFormatting);
     // END OF MY PROBLEM ---------
                   grid.EnableLinqExpressions = false;
                   grid.AllowFilteringByColumn = true;
     
                   grid.ID = "RadGrid1";
                   grid.ExportSettings.ExportOnlyData = true;
                   grid.ExportSettings.IgnorePaging = true;
                   grid.ExportSettings.OpenInNewWindow = true;
     
                   switch (reportName)
                   {
                       case ReportName.PrintTagReport:
                           grid.ExportSettings.FileName = "TagListing";
                           break;
                   }
     
                   grid.AllowSorting = true;
                   PlaceHolder1.Controls.Add(grid);
                   grid.MasterTableView.ExportToExcel();
               }
           }
  2. Wired_Nerve
    Wired_Nerve avatar
    163 posts
    Member since:
    Oct 2007

    Posted 13 Jun 2012 Link to this post

    No one has attempted to bind events to a dynaically generated radgrid from code behing before?
    I read this link    http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html   but it does not mention how to bind the excel exporting features to events in the code behind.. 

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 14 Jun 2012 Link to this post

    Hello Wareen,

    I have created the grid using Page_Init method and attaching the event works as expected.
    C#:
    protected void Page_Init(object sender, System.EventArgs e)
    {
       RadGrid RadGrid1 = new RadGrid();
       RadGrid1.ID = "RadGrid1";
       PlaceHolder1.Controls.Add(RadGrid1);
       RadGrid1.MasterTableView.DataKeyNames = new string[] { "EmployeeID" };
       RadGrid1.MasterTableView.AutoGenerateColumns = false;
       RadGrid1.MasterTableView.CommandItemSettings.ShowExportToExcelButton = true;
       RadGrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
       RadGrid1.ExportSettings.ExportOnlyData = true;
       RadGrid1.ExportCellFormatting += new EventHandler<ExportCellFormattingEventArgs>(RadGrid1_ExportCellFormatting);
       RadGrid1.ExportSettings.Excel.Format = GridExcelExportFormat.Html;
            GridBoundColumn boundColumn;
            boundColumn = new GridBoundColumn();
            RadGrid1.MasterTableView.Columns.Add(boundColumn);
            boundColumn.DataField = "EmployeeID";
            boundColumn.HeaderText = "EmployeeID";
            boundColumn = new GridBoundColumn();
            RadGrid1.MasterTableView.Columns.Add(boundColumn);
            boundColumn.DataField = "LastName";
             boundColumn.HeaderText = "LastName";
            boundColumn = new GridBoundColumn();
            RadGrid1.MasterTableView.Columns.Add(boundColumn);
    }
    void RadGrid1_ExportCellFormatting(object sender, ExportCellFormattingEventArgs e)
    {
     if (e.FormattedColumn.UniqueName == "UniqueName")
     {
    e.Cell.Style["mso-number-format"] = @"\@";  
    }
    }

    Thanks,
    Shinu.
  5. Wired_Nerve
    Wired_Nerve avatar
    163 posts
    Member since:
    Oct 2007

    Posted 14 Jun 2012 Link to this post

    Thanks Shinu, I will change my code a bit and try again.



    Warren
  6. Wired_Nerve
    Wired_Nerve avatar
    163 posts
    Member since:
    Oct 2007

    Posted 09 Jul 2012 Link to this post

    It is possible to do it besides the page_init?
  7. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 10 Jul 2012 Link to this post

    Hello Warren,

    You can create the grid entirely in page load. 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. Also check the condition (Not IsPostBack) to avoid adding the same structure objects to the grid twice.
    C#:
    protected void Page_Load(object sender, System.EventArgs e)
    {
        RadGrid RadGrid1 = new RadGrid();
        RadGrid1.ID = "RadGrid1";
        PlaceHolder1.Controls.Add(RadGrid1);
        if (!IsPostBack)
        {
            RadGrid1.DataSourceID = "SqlDataSource1";
            RadGrid1.MasterTableView.AutoGenerateColumns = false;
            RadGrid1.PageSize = 15;
            GridBoundColumn boundColumn;
            boundColumn = new GridBoundColumn();
            RadGrid1.MasterTableView.Columns.Add(boundColumn);
            boundColumn.DataField = "CustomerID";
            boundColumn.HeaderText = "CustomerID";
            boundColumn = new GridBoundColumn();
            RadGrid1.MasterTableView.Columns.Add(boundColumn);
            boundColumn.DataField = "ContactName";
            boundColumn.HeaderText = "Contact Name";
        }
    }
    Also check the following help documentation which explains the same.
    Programmatic Creation.

    Thanks,
    Shinu.
  8. Wired_Nerve
    Wired_Nerve avatar
    163 posts
    Member since:
    Oct 2007

    Posted 11 Jul 2012 Link to this post

    Thanks for the various pointers, I finally got it figured out and it was not that hard after all.


  9. Vaishali
    Vaishali avatar
    2 posts
    Member since:
    Apr 2016

    Posted 12 Apr Link to this post

    I am in a similar situation, the grid_ExportCellFormatting event is not firing, I cannot add the columns to the grid in page load or page init, because I am fetching the columns dynamically while exporting, this is the export method that I am using:

    C#

    protected void ExportExcel(RadGrid radGrid)
        {
                DataSet dsResults = GetDataSet(radGrid);
                if (dsResults != null && dsResults.Tables[0].Rows.Count > 0)
                {
                    RadGrid radGrid = new RadGrid();
                    radGrid.ID = "radGridExcel";
                    List<string> colNames = (from DataColumn x in dsResults.Tables[0].Columns select x.ColumnName).ToList();
                    radGrid.MasterTableView.Columns.Clear();
                    radGrid.EnableLinqExpressions = false;
                    colNames.ForEach(columnName => radGrid.MasterTableView.Columns.Add(new Telerik.Web.UI.GridBoundColumn { DataField = columnName, HeaderText =   columnName, AllowSorting = false, AllowFiltering = false }));
                    radGrid.AutoGenerateColumns = false;
                    radGrid.DataSource = dsResults.Tables[0];
                    radGrid.DataBind();
                    radGrid.ExportCellFormatting += new EventHandler<ExportCellFormattingEventArgs>(radGrid_ExportCellFormatting);
                    this.Controls.Add(radGrid);               
                    radGrid.ExportSettings.Excel.Format = GridExcelExportFormat.Xlsx;
                    radGrid.ExportSettings.HideStructureColumns = true;
                    radGrid.ExportSettings.OpenInNewWindow = true;
                    radGrid.ExportSettings.ExportOnlyData = true;
                    radGrid.ExportSettings.FileName = fileName;
                    radGrid.MasterTableView.GroupsDefaultExpanded = true;
                    radGrid.MasterTableView.Caption = string.Empty;
                    radGrid.MasterTableView.ExportToExcel();
                }
            }

     

    Please suggest

  10. Kostadin
    Admin
    Kostadin avatar
    1708 posts

    Posted 15 Apr Link to this post

    Hello Vaishali,

    In your case I would recommend you to use the ExportInfrastructure and manually generate the Excel document directly form your database. You can examine the following code library which demonstrates that.

    Regards,
    Kostadin
    Telerik
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017