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

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

8 Answers 489 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Wired_Nerve
Top achievements
Rank 2
Wired_Nerve asked on 12 Jun 2012, 08:53 PM
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();
           }
       }

8 Answers, 1 is accepted

Sort by
0
Wired_Nerve
Top achievements
Rank 2
answered on 13 Jun 2012, 05:14 PM
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.. 

0
Shinu
Top achievements
Rank 2
answered on 14 Jun 2012, 05:12 AM
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.
0
Wired_Nerve
Top achievements
Rank 2
answered on 14 Jun 2012, 01:29 PM
Thanks Shinu, I will change my code a bit and try again.



Warren
0
Wired_Nerve
Top achievements
Rank 2
answered on 09 Jul 2012, 11:11 PM
It is possible to do it besides the page_init?
0
Accepted
Shinu
Top achievements
Rank 2
answered on 10 Jul 2012, 06:21 AM
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.
0
Wired_Nerve
Top achievements
Rank 2
answered on 11 Jul 2012, 02:43 PM
Thanks for the various pointers, I finally got it figured out and it was not that hard after all.


0
Vaishali
Top achievements
Rank 1
answered on 12 Apr 2016, 11:57 AM

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

0
Kostadin
Telerik team
answered on 15 Apr 2016, 11:10 AM
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.
Tags
Grid
Asked by
Wired_Nerve
Top achievements
Rank 2
Answers by
Wired_Nerve
Top achievements
Rank 2
Shinu
Top achievements
Rank 2
Vaishali
Top achievements
Rank 1
Kostadin
Telerik team
Share this question
or