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

Export to Excel - Change GroupHeaderRow and GroupFooterRow's Style for each group

2 Answers 95 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Software
Top achievements
Rank 1
Software asked on 27 Nov 2017, 12:32 PM

Hello,

I would like to change the style of GroupHeaderRow and GroupFooterRow for each group in ElementExportingToDocument event.

Could you help me to to this please ?

Thanks a lot

Regards.

ie : 

private void ElementExportingToDocument(object sender, GridViewElementExportingToDocumentEventArgs e)
        { if (e.Element == ExportElement.GroupHeaderRow )
            {
//-----------------------------------------------------
// How to know here in witch group I am ??
//-----------------------------------------------------
Color MyColorGroup = ColorSwitchGroup(thisgroup);
 
                (e.VisualParameters as GridViewDocumentVisualExportParameters).Style = new CellSelectionStyle()
               {
                   FontSize = UnitHelper.PointToDip(11),
                   IsBold = true,
 
 
                   Fill = new PatternFill(PatternType.Solid, MyColorGroup ,MyColorGroup ),
                   CellBorders = CellBorders.CreateOutline(new CellBorder(CellBorderStyle.Thin, new ThemableColor(Colors.Black)))
               };
            }
            else if (e.Element == ExportElement.GroupFooterRow)
            {
//-----------------------------------------------------
// How to know here in witch group I am ??
//-----------------------------------------------------
Color MyColorGroup = ColorSwitchGroup(thisgroup);
                (e.VisualParameters as GridViewDocumentVisualExportParameters).Style = new CellSelectionStyle()
                {
                    FontSize = UnitHelper.PointToDip(11),
                    IsBold = true,
                    Fill = new PatternFill(PatternType.Solid, MyColorGroup , MyColorGroup ),
                    CellBorders = CellBorders.CreateOutline(new CellBorder(CellBorderStyle.Thin, new ThemableColor(Colors.Black)))
                };
            }
   }

2 Answers, 1 is accepted

Sort by
0
Vladimir Stoyanov
Telerik team
answered on 30 Nov 2017, 10:49 AM
Hello Olivier,

For the GroupHeaderRows you can cast the GridViewElementExportingToDocumentEventArgs' DataContext to a QueryableCollectionViewGroup and then use its Key property.

As for the GroupFooterRows you can cast the DataContext to an AggregateResultCollection and then match it with the correct group from the all the groups in the RadGridView. Here is how that would look:

private void RadGridView_ElementExportingToDocument(object sender, Telerik.Windows.Controls.GridViewElementExportingToDocumentEventArgs e)
        {
            if (e.Element == ExportElement.GroupHeaderRow)
            {
                var group = e.DataContext as QueryableCollectionViewGroup;
                var key = group.Key;
 
                //Color MyColorGroup = ColorSwitchGroup(thisgroup);
                (e.VisualParameters as GridViewDocumentVisualExportParameters).Style = new CellSelectionStyle()
                {
                    FontSize = UnitHelper.PointToDip(11),
                    IsBold = true,
                    //Fill = new PatternFill(PatternType.Solid, MyColorGroup, MyColorGroup),
                    CellBorders = CellBorders.CreateOutline(new CellBorder(CellBorderStyle.Thin, new ThemableColor(Colors.Black)))
                };
            }
            else if (e.Element == ExportElement.GroupFooterRow)
            {
                var group = e.DataContext as AggregateResultCollection;
                var groups = this.gridViewExport.Items.Groups;
                QueryableCollectionViewGroup currentGroup = GetCurrentGroup(group, groups);
                var key = currentGroup.Key;
 
                //Color MyColorGroup = ColorSwitchGroup(thisgroup);
                (e.VisualParameters as GridViewDocumentVisualExportParameters).Style = new CellSelectionStyle()
                {
                    FontSize = UnitHelper.PointToDip(11),
                    IsBold = true,
                    //Fill = new PatternFill(PatternType.Solid, MyColorGroup, MyColorGroup),
                    CellBorders = CellBorders.CreateOutline(new CellBorder(CellBorderStyle.Thin, new ThemableColor(Colors.Black)))
                };
            }
        }

Here is the method for recursively getting the correct group from all of the RadGridView groups:

private QueryableCollectionViewGroup GetCurrentGroup(AggregateResultCollection aggregateResults, IEnumerable<object> groups)
       {
           foreach (QueryableCollectionViewGroup group in groups)
           {
               if (group.AggregateResults == aggregateResults)
               {
                   return group;
               }
               else if (group.HasSubgroups)
               {
                   var foundSubgroup = GetCurrentGroup(aggregateResults, group.Subgroups);
                   if (foundSubgroup != null)
                   {
                       return foundSubgroup;
                   }
               }
           }
 
           return null;
       }

Please let me know if such an approach would be suitable for your scenario. If that is not the case, please provide more details on your exact requirement and I will gladly assist you further.

Regards,
Vladimir Stoyanov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Software
Top achievements
Rank 1
answered on 30 Nov 2017, 02:07 PM
Great ! Exactly what I need.
Thank you Vladimir 
Regards.
Olivier.
Tags
GridView
Asked by
Software
Top achievements
Rank 1
Answers by
Vladimir Stoyanov
Telerik team
Software
Top achievements
Rank 1
Share this question
or