$99 VS $1000+. UI Control for ASP.NET AJAX(contains chart and gridview).
GridView WinForms Control
Export grid data to excel, CSV, HTML, and PDF files in C# WinForms Applications.
Home > WinForms UI Controls > User Manual > Export Group Data to Excel in C#

Export Data in a Group to Excel from GridView

The Kettic GridView .NET WinForms control supports not only exporting data to Excel, but also exporting data in a group from the grouped Kettic GridView to Excel. This is achieved by using the context menu and the C# API included in it. This feature needn’t the MS Office installation, however, allows the exported Excel file being read in MS Excel spreadsheet 2003 and higher versions. This article will talk about how to export data in a group to Excel in C#.NET

How to Export Data in a Group to Excel in C#.NET

First, we need to create the Kettic ContextMenu and get the group header row through the methods, MouseDown and GetGridGroupHeaderRowElement. When determining the clicked group row, we can get the child rows of the grouped rows. The following is the C# code that demonstrates how to achieve this.
The ExportToExcel method does not support data exporting to Excel when there is no data specified, so we need to use the ExportToExcelML C# class with DoNotExport for HiddenRowOption property.
In the Kettic GridView.Rows collection, we need to mark the rows matching that of the child collection through changing the Visible property to True and disable the rest. And then change the HiddenRowOption to DoNotExport to export the Kettic GridView. The C# code below shows how to accomplish this.

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.DisplayDialog() == DialogResult.OK)
{
foreach (GridViewRowInformation row in this.ketticGridView1.Rows)
{
bool isVisible = false;
foreach (GridViewRowInformation childRow in this.childRowsForExport)
{
if (row == childRow)
{
isVisible = true;
}
}

row.IsVisible = isVisible;
}

ExportToExcelML exporter = new ExportToExcelML(this.ketticGridView1);
exporter.HiddenRowOption = HiddenOption.DoNotExport;
exporter.RunExport(dialog.FileName);
MessageBox.Display("Exporting Done");
}
foreach (GridViewRowInformation row in this.ketticGridView1.Rows)
{
row.Visible = true;
}
}

KetticMenuItem menuItem = (KetticMenuItem)sender;
menuItem.Click -= contextMenuItem_Click;
this.childRowsForExport = null;
}
The C# code below will allows the users to get the text from the GroupRowHeader.

private string GetGroupLineHeaderText(GridGroupHeaderRowElement headerRowElement)
{
string headerText = headerRowElement.RowInformation.Group.GroupRow.HeaderText;
return headerText;
}
UI Controlsfor Windows Forms
.NET WinForms UI Overview.NET WinForms UI Features.NET WinForms UI GuideC# WinForms UI DesignVB.NET WinForms UI Design
WinForms UI Controls