Articles in this section

How to export two grids in a single worksheet of a workbook in WinForms GridGroupingControl?

Excel export


By default, the
WinForms GridGroupingControl does not have direct support to export multiple grids as same as in view (with styles and formats) into a single worksheet in the workbook. To perform this, create a custom GridGroupingExcelConverterControl class. Set proper value to the ExcelStartRowIndex property of the worksheet in the ExportTable overridden method.


C#
 

//Creating custom class
public class GridGroupingExcelConverterControlEXT : GridGroupingExcelConverterControl
{
    private int rowIndex = 1;
    
    public GridGroupingExcelConverterControlEXT()
    {
    }

    public int ExcelStartRowIndex { get { return rowIndex; } set { rowIndex = value; } }

    protected override void ExportTable(IList tableElements, GridTableDescriptor tableDescriptor, ref int excelRowIndex, int excelStartColumnIndex, bool isNestedTable, IWorksheet sheet, ExcelExportingOptions exportingOptions)
    {
        excelRowIndex = ExcelStartRowIndex;
        base.ExportTable(tableElements, tableDescriptor, ref excelRowIndex, excelStartColumnIndex, isNestedTable, sheet, exportingOptions);
    }
}

//Exporting to Excel
private void exportBtn_Click(object sender, EventArgs e)
{
     ExcelEngine Engine = new ExcelEngine();
     IWorkbook workbook = Engine.Excel.Workbooks.Create(1);
     IWorksheet sheet = workbook.Worksheets[0];
     sheet.Name = "sample";
     ExcelExportingOptions options = new ExcelExportingOptions();
     GridGroupingExcelConverterControlEXT converter = new GridGroupingExcelConverterControlEXT();

     //Exporting 1st grid
     converter.ExportToExcel(gridGroupingControl1, sheet, options);

     //Specifying the starting row index of 2nd grid
     converter.ExcelStartRowIndex = sheet.UsedRange.LastRow + 2;

     //Exporting 2nd grid
     converter.ExportToExcel(gridGroupingControl2, sheet, options);
     workbook.SaveAs("Sample.xls");
     Process.Start("Sample.xls");
}
VB
'Creating custom class 
Public Class GridGroupingExcelConverterControlEXT
    Inherits GridGroupingExcelConverterControl
    
    Private rowIndex As Integer = 1
    
    Public Sub New()
    End Sub
    
    Public Property ExcelStartRowIndex() As Integer
        Get
            Return rowIndex
        End Get
        Set(ByVal value As Integer)
            rowIndex = value
        End Set
    End Property
    
    Protected Overrides Sub ExportTable(ByVal tableElements As IList, ByVal tableDescriptor As GridTableDescriptor, ByRef excelRowIndex As Integer, ByVal excelStartColumnIndex As Integer, ByVal isNestedTable As Boolean, ByVal sheet As IWorksheet, ByVal exportingOptions As ExcelExportingOptions)
        excelRowIndex = ExcelStartRowIndex
        MyBase.ExportTable(tableElements, tableDescriptor, excelRowIndex, excelStartColumnIndex, isNestedTable, sheet, exportingOptions)
    End Sub
End Class

'Exporting to Excel
Private Sub exportBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim Engine As New ExcelEngine()
    Dim workbook As IWorkbook = Engine.Excel.Workbooks.Create(1)
    Dim sheet As IWorksheet = workbook.Worksheets(0)
    sheet.Name = "sample"

    ' save to workbook in excel
    Dim options As New ExcelExportingOptions()
    Dim converter As New GridGroupingExcelConverterControlEXT()

    'Exporting 1st grid
    converter.ExportToExcel(gridGroupingControl1, sheet, options)
    'Specifying the starting row index of 2nd grid
    converter.ExcelStartRowIndex = sheet.UsedRange.LastRow + 2

    'Exporting 2nd grid
    converter.ExportToExcel(gridGroupingControl2, sheet, options)
    workbook.SaveAs("Sample.xls")
    Process.Start("Sample.xls")
End Sub
Export Multiple grids into single worksheet as shown in the screenshot below


Export multiple grids


Export GridGroupingControl to Excel sheet

 

Samples:

C#: Export multiple grid_CS

VBExport multiple grid_VB

 

Reference Link: Excel Export

 

Conclusion


I hope you enjoyed learning about how to export two grids in a single worksheet of a workbook in GridGroupingControl.

You can refer to our  WinForms GridGroupingControl feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms GridGroupingControl documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you! 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied