This is a migrated thread and some comments may be shown as answers.

Excel export Gridview contents when grouped

3 Answers 47 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Oliver
Top achievements
Rank 1
Oliver asked on 01 Sep 2011, 03:44 AM
I'm reading the contents into an array then assigning it excel range for faster export.
I successfully exported to excel when it is not grouped. However when grouped I dont get the grouping effect appearing.
Below is a snippet:

           foreach (GridViewColumn col in grid.Columns)
                {
                    arr[0, i] = col.HeaderText;
                    i++;
                }
 
                //Contents
                long currRow = 0;
                foreach (GridViewRowInfo row in grid.Rows)
                {
                    int currCol=0;
                    foreach (GridViewColumn col in grid.Columns)
                    {
                        if(row.Cells[currCol].Value != null )
                        {
                           arr[currRow + 1, currCol] = row.Cells[currCol].Value.ToString();
                        }
                        else
                        {
                            arr[currRow + 1, currCol] ="";
                        }
 
                    currCol ++;
                    }
                    currRow ++;
                }
 
                rng.Value2 = arr;

any suggestions will be appreciated.

3 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 02 Sep 2011, 02:10 PM
Hello Oliver,

The Rows collection of RadGridView contains all rows as they appear in the underlying data source. You should use the ChildRows collection instead, it contains the rows as they appear in RadGridView (after applying sorting, grouping or filtering operations. This collection contains all group rows when grouping applies. Here is an example on how to iterate group rows:
IterateRows(this.radGridView1.ChildRows);
 
private void IterateRows(GridViewChildRowCollection rows)
{
    foreach (GridViewRowInfo row in rows)
    {
        ExportRow(row);
        IterateRows(row.ChildRows);
    }
}

I hope this helps.
 
Kind regards,
Jack
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Oliver
Top achievements
Rank 1
answered on 06 Sep 2011, 04:04 AM
I've got the ExportRow to output the group header, if it is a group, and print out the row details if it is not 
down to the lowest level.
The currRow is just to keep track of the current row. Im using this variable
as an array row reference also.

Is this the correct way of dealing with it?



public static long currRow;

private
 void ExportRow(GridViewRowInfo row)         {             if (row.Group != null)             {                 if (row.HierarchyLevel == row.Group.GroupRow.HierarchyLevel)                 {                     Console.WriteLine(row.Group.GroupRow.HeaderText);                 }                 else                 {                     foreach (GridViewCellInfo cell in row.Cells)                     {                         Console.WriteLine(cell.Value);                     }                     currRow++;                 }             }             else             {                 foreach (GridViewCellInfo cell in row.Cells)                 {                     Console.WriteLine(cell.Value);                 }                 currRow++;             }         }
0
Jack
Telerik team
answered on 08 Sep 2011, 02:05 PM
Hi Oliver,

I am glad to hear that you have found a solution and thank you for sharing it with the community. Yes, this is a correct approach. If you have any further questions, do not hesitate to ask.
 
Best wishes,
Jack
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
GridView
Asked by
Oliver
Top achievements
Rank 1
Answers by
Jack
Telerik team
Oliver
Top achievements
Rank 1
Share this question
or