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

Hide Detail table headers on Excel export when there are no data

3 Answers 127 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ernest Mombay
Top achievements
Rank 1
Ernest Mombay asked on 28 Dec 2010, 11:01 PM
Hi,

Is there a way to hide a detail table's headers during Excel export when there is no data for that detail table? I've set the setting
ShowHeadersWhenNoRecords="false"
on the detail tables. This works fantastic on the PDF export, but for some reason, the headers still display on the Excel export. The layout of the our grid is a master table with two detail tables underneath, to show different properties of the parent item. I bind all the tables dynamically, using OnNeedDatasource for the main table and OnDetailTableBind on the detail tables. The detail tables are not shown in the page, and only appear during the grid export.

Any help would be appreciated.
Ernest

3 Answers, 1 is accepted

Sort by
0
Accepted
Daniel
Telerik team
answered on 29 Dec 2010, 10:37 PM
Hello Ernest,

Please test the attached project and let me know whether the demonstrated approach is suitable for your scenario.

Regards,
Daniel
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Ernest Mombay
Top achievements
Rank 1
answered on 30 Dec 2010, 12:47 AM

Thanks for your quick response. I looked at the major change from your code, and I tried to implement it to our solution but it did not work. What was basically new is what is coded below:

protected void grdAllView_ExcelMLExportRowCreated(object sender, GridExportExcelMLRowCreatedArgs e)
    {
        string data = "";
  
        if (e.RowType == GridExportExcelMLRowType.HeaderRow)
        {
            foreach (CellElement cell in e.Row.Cells)
                data += cell.Data.DataItem.ToString();
  
            if (String.IsNullOrEmpty(data.Trim()))
                e.Worksheet.Table.Rows.Remove(e.Row);
        }
    }

When stepping through, I noticed that even Header rows still contained data (which is the header text) and therefore it was never removed. Then I assumed that to create a valid worksheet, a HeaderRow is created before DataRows, and that HeaderRows cannot be contiguous. So I introduced a global variable that keeps tabs on the previous row type and checks if Header rows are adjacent. So my code is now: 

protected void grdAllView_ExcelMLExportRowCreated(object sender, GridExportExcelMLRowCreatedArgs e)
    {
       if (e.RowType == GridExportExcelMLRowType.HeaderRow && e.RowType == _lastExcelRowType)
                if (e.Worksheet.Table.Rows.Count > 3)
                    e.Worksheet.Table.Rows.Remove(e.Worksheet.Table.Rows[e.Worksheet.Table.Rows.Count - 2]);
       _lastExcelRowType = e.RowType; //_lastExcelRow is the counter for the previous row type.
    }

And everything seems to work great. I appreciate your help!
Ernest
0
Daniel
Telerik team
answered on 04 Jan 2011, 11:53 AM
Hello Ernest,

The header rows are empty in my project since I set the datasource to an empty table:
protected void grdAllView_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
{
    if (e.DetailTableView.ParentItem.ItemIndex == 1)
        e.DetailTableView.DataSource = GetData(1);
    else
        e.DetailTableView.DataSource = new DataTable();
}

If you modify this code to return GetData(0) the header item will be populated which is not desired in this case.

Best regards,
Daniel
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Grid
Asked by
Ernest Mombay
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Ernest Mombay
Top achievements
Rank 1
Share this question
or