In my application, I have a grid with a maximum of two bands. When the user makes a context menu selection on band[0] to export to excel, all selected rows in band[0] will be exported along with any child rows in band[1]. When I do this, I get extra ColumnHeader rows in my excel. Is there a way to only get the ColumnHeader rows for the for the rows that I am exporting?
Here is the code I am using to handle the selected row export:
private void ultraGridExcelExporter1_RowExporting(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.RowExportingEventArgs e) { UltraGrid grid = e.GridRow.Band.Layout.Grid as UltraGrid; UltraGridRow realGridRow = grid.GetRowFromPrintRow(e.GridRow); if (realGridRow.Band.Index == 0 && realGridRow.Selected == false) { e.Cancel = true; } else if (realGridRow.Band.Index == 1 && realGridRow.ParentRow.Selected == false) { e.Cancel = true; } }
Any help is appreciated.
Thanks,
Justin
Hi Justin,
Instead of using the RowExporting event, use the InitializeRow event on the UltraGridExcelExporter, instead. Note that this is an event on the Export, I am not referring to the InitializeRow event on the grid itself.
Then you can do essentially the same code as above, excelt instead of setting e.Cancel to true, you set e.SkipRow to true to skip the row.
After that, you can check the child rows and see if none of them are selected. To get the child rows, you would do something like this:
e.Row.ChildBands[0].Rows
Loop through these rows and get each one's corresponding "real" grid row using GetRowFromPrintRow and see if it is selected.
If NONE of the child rows are selected, you can set e.SkipDescendants to true to cancel the export of all of the child rows - this should also account for the header.
An alternative method would be to use the HeaderExporting event and use the GridRow there to get the rows collection and do the same thing - loop through all the rows in the collection and see if any are selected and if not, cancel the header export.
The first method is better, though, because it will also account for summaries and anything else that might be exported along with the rows.
After playing with this a bit more, I noticed that I was getting extra group by rows in my export. I solved this by using the following code. It seems to cover all of the scenarios that my application runs into. Initially I was worried about performance issues from iterating child rows, however; as long as foundSelectedRows == false is the common scenario (as it is in my app), the performance hit shouldn't be too large. Thanks for the help.
private void ultraGridExcelExporter1_InitializeRow(object sender, ExcelExportInitializeRowEventArgs e) { bool foundSelectedRows = false; UltraGrid grid = e.Row.Band.Layout.Grid as UltraGrid; UltraGridRow realGridRow = grid.GetRowFromPrintRow(e.Row); if (realGridRow.IsGroupByRow) { foreach (UltraGridChildBand band in realGridRow.ChildBands) { foreach(UltraGridRow row in band.Rows) { if (row.Selected) { foundSelectedRows = true; break; } } } if (foundSelectedRows == false) { e.SkipRow = true; e.SkipDescendants = true; } } else if (realGridRow.Band.Index == 0 && realGridRow.Selected == false) { e.SkipRow = true; e.SkipDescendants = true; } }
Hi
I am Dhanraj.
I implement the above code successfully selected rows exported.
but didn't show the header name of particular child.
Pls help me..
Could you post a sample which reproduces the unwanted behavior?
-Justin
Hi Fasiha,
I'm not sure I understand your question. The code you have here is always setting SkipRow and SkipDescendants to false, so this will essentially do nothing.
Hi,
I'm used Initialize Row event to export child bands in excel. I'm getting the output. But my problem is that it dosen't exporting Column header of child bands. Am i missing any thing?
//To export child band in excel
private static void excelExporter_InitializeRow(object sender, ExcelExportInitializeRowEventArgs e) { UltraGrid grid = e.Row.Band.Layout.Grid as UltraGrid; UltraGridRow realGridRow = grid.GetRowFromPrintRow(e.Row); e.Row.Band.Header.Activated = true; if (realGridRow.Band.Index == 1 && realGridRow.Selected == false) { e.SkipRow = false; e.SkipDescendants = false; } else if (realGridRow.Band.Index == 2 && realGridRow.Selected == false) { e.SkipRow = false; e.SkipDescendants = false; } }
Thanks & Regards,
Fasiha