New to Telerik UI for WinForms? Download free 30-day trial

Export Data in a Group to Excel

Sometimes you may need to export just the data in a group in a grouped RadGridView. This is possible by using the context menu:

1. Create RadContextMenu and get the right-clicked group header row by using the MouseDown event and the GetGridGroupHeaderRowElement method. After determining the clicked group row, you can get its child rows. Then simply show the context menu:

GridViewChildRowCollection childRowsForExport;
void radGridView1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        GridGroupHeaderRowElement headerRowElement = this.GetGridGroupHeaderRowElement(e.Location);
        RadContextMenu radContextMenu1 = new RadContextMenu();
        RadMenuItem contextMenuItem = new RadMenuItem();

        contextMenuItem.Text = "Export to Excel";
        contextMenuItem.Click += new EventHandler(contextMenuItem_Click);
        radContextMenu1.Items.Add(contextMenuItem);
        if (headerRowElement != null)
        {
            this.childRowsForExport = headerRowElement.RowInfo.ChildRows;
            radContextMenu1.DropDown.Location = this.PointToScreen(e.Location);
            radContextMenu1.Show();
        }
    }
}
private GridGroupHeaderRowElement GetGridGroupHeaderRowElement(Point location)
{
    RadElement elementUnderMouse = this.radGridView1.ElementTree.GetElementAtPoint(location);
    while (elementUnderMouse != null)
    {
        GridGroupHeaderRowElement headerRow = elementUnderMouse as GridGroupHeaderRowElement;
        if (headerRow != null)
        {
            return headerRow;
        }
        elementUnderMouse = elementUnderMouse.Parent;
    }
    return null;
}

2. Since the export to excel methods do not support exporting only specified records, you can work-around this if you temporarily hide the unnecessary rows and use ExportToExcelML class with the DonNotExport for HiddenRowOption properties.

3. In the click event of the context menu item, mark the rows in the RadGridView.Rows collection that match the rows of the child collection by setting their IsVisible property to true, and all the rest to false. Then simply export RadGridView with HiddenRowOption set to HiddenOption.DoNotExport:

void contextMenuItem_Click(object sender, EventArgs e)
{
    if (this.childRowsForExport != null && this.childRowsForExport.Count > 0)
    {
        SaveFileDialog dialog = new SaveFileDialog();
        dialog.Filter = "Excel File (*.xls)|*.xls";
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            //hide rows, which should not be exported
            foreach (GridViewRowInfo row in this.radGridView1.Rows)
            {
                bool isVisible = false;
                foreach (GridViewRowInfo childRow in this.childRowsForExport)
                {
                    if (row == childRow)
                    {
                        isVisible = true;
                    }
                }
                row.IsVisible = isVisible;
            }
            ExportToExcelML exporter = new ExportToExcelML(this.radGridView1);
            exporter.HiddenRowOption = HiddenOption.DoNotExport;
            exporter.RunExport(dialog.FileName);
            MessageBox.Show("Export Finished");
        }
        //show all rows
        foreach (GridViewRowInfo row in this.radGridView1.Rows)
        {
            row.IsVisible = true;
        }
    }
    //release resources
    RadMenuItem menuItem = (RadMenuItem)sender;
    menuItem.Click -= contextMenuItem_Click;
    this.childRowsForExport = null;
}

4. If you need to get the text from the GroupRowHeader, use the following snippet:

private string GetGroupLineHeaderText(GridGroupHeaderRowElement headerRowElement)
{
    string headerText = headerRowElement.RowInfo.Group.GroupRow.HeaderText;
    return headerText;
}