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

radgrid excel export missing column headers

2 Answers 352 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Donna
Top achievements
Rank 1
Donna asked on 26 Jul 2017, 07:31 PM

I am troubleshooting a program that someone else wrote and there is a grid that is missing a few column headers upon export to excel.  The column headers and data showed up on screen but not on excel export.  The first few columns would have headers and the rest of the columns displayed data but no headers.  In the method that gets called by OnItemCommand, I have tried changing the header text by doing this:

gridMasterTableView.GetColumn(I).HeaderText = I;

but it doesn't seem to change anything on the excel.

In the method that is called by OnItemDataBound I tried to put a breakpoint on this line:

GridHeaderItem header = (GridHeaderItem)e.Item;

In the GridHeaderItem I noticed it only has a few of the columns.

I just want to fill the missing column headers.  Is there a good place where I can access the datatable that gets exported to excel and put in the headers?  In other words, if I only want to control what column name to put on excel export without touching the original source data, what would be a good place to do it and how to go about doing that?

Thanks.

2 Answers, 1 is accepted

Sort by
0
Donna
Top achievements
Rank 1
answered on 26 Jul 2017, 08:11 PM

upon further investigation, I noticed that on the aspx page mastertableview is missing the columns that require headings.  The ones that have headings on the excel file, their columns are specified on the aspx page. 

Those columns missing headers were added in the code behind during grid binding:

 

for (int i = 2011; i <= currentyear; i++)

 

 

{

 

GridNumericColumn boundColumn = new GridNumericColumn();

boundColumn.DataField = ""+i;

boundColumn.HeaderText = ""+i;

boundColumn.UniqueName = ""+i;

 

 

 

boundColumn.Aggregate = GridAggregateFunction.Sum;

boundColumn.FooterText = " ";

boundColumn.HeaderStyle.Width = new Unit(65);

boundColumn.HeaderText = ""+i;

boundColumn.DataField = "" + i;

boundColumn.ReadOnly = true;

boundColumn.UniqueName = "" + i;

boundColumn.DataType = typeof(System.Double);

//boundColumn.ColumnType =

boundColumn.NumericType = NumericType.Currency;

boundColumn.FooterAggregateFormatString = "{0:C}";

 

 

 

additionaFieldsList.Add("["+i+"]");

 

 

grid.MasterTableView.Columns.Add(boundColumn);

}

 

Would adding columns from code behind wipe out the column headers?

Would rebinding the grid at later point wipe out the headers?

grid.Rebind();

I am just throwing ideas around as I'm at a lost on how to make the column headers show up on the excel export file.

 

 

0
Eyup
Telerik team
answered on 31 Jul 2017, 12:35 PM
Hi Donna,

Please note that mixing declarative columns defined in the aspx page with programmatically added ones is not supported. If you need to create the grid programmatically, please create the entire grid on the code-behind during the Page_Init event handler. In the aspx page you can put a asp:PlaceHolder element. You can check the following section for instructions:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/defining-structure/creating-a-radgrid-programmatically#creating-a-radgrid-on-page_init

Also, make sure you are not using DataBind() method to bind the grid. Performing complex grid operations such as Inserting, Deleting, Updating, Hierarchy relations, Grouping, Exporting, Paging, Sorting, Filtering, etc. require accommodating appropriate database operations.  Therefore, we suggest you to avoid Simple Databinding and strongly recommend the use of more advanced databinding methods, which automatically handle the aforementioned functions:

Declarative DataSource (DataSourceID property)
Programmatic Data Binding (NeedDataSource event, + DetailTableDataBind for hierarchy). You should set the DataSource property ONLY within these event handlers.


I hope this will prove helpful.

Regards,
Eyup
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Donna
Top achievements
Rank 1
Answers by
Donna
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or