Excel export Gridview contents when grouped

4 posts, 0 answers
  1. Oliver
    Oliver avatar
    33 posts
    Member since:
    Jan 2011

    Posted 31 Aug 2011 Link to this post

    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.
  2. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 02 Sep 2011 Link to this post

    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 >>

  3. Oliver
    Oliver avatar
    33 posts
    Member since:
    Jan 2011

    Posted 05 Sep 2011 Link to this post

    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++;             }         }
  4. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 08 Sep 2011 Link to this post

    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 >>

Back to Top