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...
protectedvoidgrid_ExportCellFormatting(objectsource, 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 TIPWebITprotectedvoidCreateGrid(){using(RadGrid grid =newRadGrid()){grid.NeedDataSource +=newGridNeedDataSourceEventHandler(grid_NeedDataSource);grid.ExcelMLExportRowCreated +=newGridExcelMLExportRowCreatedEventHandler(grid_ExcelMLExportRowCreated);grid.ExcelMLExportStylesCreated +=newGridExcelMLExportStylesCreatedEventHandler(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 +=newEventHandler<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){caseReportName.PrintTagReport:grid.ExportSettings.FileName ="TagListing";break;}grid.AllowSorting =true;PlaceHolder1.Controls.Add(grid);grid.MasterTableView.ExportToExcel();}}
8 Answers, 1 is accepted
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..
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.
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"; }}Programmatic Creation.
Thanks,
Shinu.
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
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