Hello Satish.
If your Datagrids are filled with similar Datasets, then just add another Iteration over your existing Excel filling.
Dim i As Integer, j As Integer, DatagridCycle as Integer
This will be then the counter for each Cycle of the Datagrids.
If needed, store the last Datagrid Index or add just a Counter for each Datagrid for Offset(s).
Dim i As Integer, j As Integer, DatagridCycle as Integer
For DatagridCycle = 0 to 2 then
Dim diff As Integer = 1
For j = 0 To DataGridView1.ColumnCount - 1
Select Case DatagridCycle
Case 0
excel.Worksheets(1).cells(1, j + 1) = DataGridView1.Columns(j).Name
Case 1
excel.Worksheets(1).cells(1, j + 1) = DataGridView2.Columns(j).Name
Case 2
excel.Worksheets(1).cells(1, j + 1) = DataGridView3.Columns(j).Name
End Select
Next
diff += 1
For i = 0 To DataGridView4.RowCount - 1
If DataGridView4.Rows(i).IsNewRow = False Then
For j = 0 To DataGridView4.ColumnCount - 1
excel.Worksheets(1).cells(i + diff, j + 1) = DataGridView4.Item(j, i).Value
Next
Next
Question? I asume You store in
diff
the next Column position?
If so, and you want to extend the Excel-Table later, you will need an entrypoint (Offset) where to restart.
You could store it inside another Variable or the most effective way is to use Excel API/Interop.
For that i would request inside the Target Table the next free cell from the Top, store then the index value inside your Application and use that as Offset.
excel.Worksheets(1).cells(1,offset+ j + 1) = DataGridView1.Columns(j).Name
I used that Years ago... But was tricky in the first times to get on with EXCEL Interop.
I used always a Representation of the Cell Positions inside my Application for later use.
I hope that will help.
BTW this is my first Quick Response. ;)