diff --git a/GridView/ExportHierarchy/ExportHierarchy_WPF.csproj b/GridView/ExportHierarchy/ExportHierarchy_WPF.csproj index 301144bf..a50d6178 100644 --- a/GridView/ExportHierarchy/ExportHierarchy_WPF.csproj +++ b/GridView/ExportHierarchy/ExportHierarchy_WPF.csproj @@ -126,6 +126,7 @@ + @@ -150,6 +151,7 @@ Settings.Designer.cs + diff --git a/GridView/ExportHierarchy/HierarchicalExportGridView.cs b/GridView/ExportHierarchy/HierarchicalExportGridView.cs index 8a4ee793..857ef98b 100644 --- a/GridView/ExportHierarchy/HierarchicalExportGridView.cs +++ b/GridView/ExportHierarchy/HierarchicalExportGridView.cs @@ -18,14 +18,34 @@ namespace ExportHierarchy public class HierarchicalExportGridView : RadGridView { private int subItemsCount = 0; - private Dictionary subItemsDictionary = new Dictionary(); + private List parentItemsDictionary = new List(); private int headerRowCount; public HierarchicalExportGridView() { + this.ElementExportingToDocument += HierarchicalExportGridView_ElementExportingToDocument; this.ElementExportedToDocument += HierarchicalExportGridView_ElementExportedToDocument; } + private void HierarchicalExportGridView_ElementExportingToDocument(object sender, GridViewElementExportingToDocumentEventArgs e) + { + if (e.Element == ExportElement.HeaderRow) + { + (e.VisualParameters as GridViewDocumentVisualExportParameters).Style = new CellSelectionStyle() + { + IsBold = true, + Fill = new PatternFill(PatternType.Solid, Color.FromArgb(255, 232, 232, 232), Colors.Transparent), + }; + } + else if (e.Element == ExportElement.Row) + { + (e.VisualParameters as GridViewDocumentVisualExportParameters).Style = new CellSelectionStyle() + { + Fill = new PatternFill(PatternType.Solid, Color.FromArgb(255, 255, 142, 142), Colors.Transparent), + }; + } + } + private void HierarchicalExportGridView_ElementExportedToDocument(object sender, GridViewElementExportedToDocumentEventArgs e) { if (e.Element == ExportElement.Row && e.DataContext != null) @@ -39,11 +59,19 @@ namespace ExportHierarchy var subItems = item.GetType().GetProperty(property).GetValue(item) as IList; if (subItems.Count > 0) { - var index = gridView.Items.IndexOf(item) + 1 + subItemsCount; + var originalIndex = gridView.Items.IndexOf(item); + var newIndex = originalIndex + subItemsCount; subItemsCount += subItems.Count; - this.subItemsDictionary.Add(index, subItems); + this.parentItemsDictionary.Add(new ParentExportInfo() + { + OriginalIndex = originalIndex, + ExportIndex = newIndex, + SubItems = subItems + }); } } + + } } @@ -78,7 +106,7 @@ namespace ExportHierarchy } this.subItemsCount = 0; - this.subItemsDictionary.Clear(); + this.parentItemsDictionary.Clear(); } } @@ -90,29 +118,48 @@ namespace ExportHierarchy workbook = this.ExportToWorkbook(); var worksheet = workbook.ActiveWorksheet; - DataTemplate dt = this.HierarchyChildTemplate; - DependencyObject dio = dt.LoadContent(); - var childGrid = dio as RadGridView; + worksheet.GroupingProperties.SummaryRowIsBelow = false; + DataTemplate template = this.HierarchyChildTemplate; + DependencyObject content = template.LoadContent(); + var childGrid = content as RadGridView; + var parentItemCount = 0; - foreach (var subItem in subItemsDictionary) + foreach (var parentItem in parentItemsDictionary) { - var rowIndex = subItem.Key; - RowSelection selection = worksheet.Rows[rowIndex + 1, rowIndex + subItem.Value.Count]; + var rowIndex = parentItem.ExportIndex + headerRowCount + parentItemCount + 1; + parentItemCount++; + RowSelection selection = worksheet.Rows[rowIndex, rowIndex + parentItem.SubItems.Count]; selection.Insert(); - for (var i = 0; i < subItem.Value.Count; i++) + for (var j = 0; j < childGrid.Columns.Count; j++) { - var item = subItem.Value[i]; - for (var j = 0; j < childGrid.Columns.Count; j++) + var column = childGrid.Columns[j] as GridViewDataColumn; + var header = column.Header != null ? column.Header.ToString() : column.DataMemberBinding.Path.Path; + var headerCell = worksheet.Cells[rowIndex, j]; + headerCell.SetValueAsText(header); + var headerCellFill = new PatternFill(PatternType.Solid, Color.FromArgb(255, 150, 150, 150), Colors.Transparent); + headerCell.SetFill(headerCellFill); + headerCell.SetIsBold(true); + + for (var i = 0; i < parentItem.SubItems.Count; i++) { - var column = childGrid.Columns[j] as GridViewDataColumn; + var item = parentItem.SubItems[i]; + var cell = worksheet.Cells[rowIndex + 1 + i, j]; var property = item.GetType().GetProperty(column.DataMemberBinding.Path.Path); var value = property != null ? property.GetValue(item).ToString() : string.Empty; cell.SetValueAsText(value); - var solidPatternFill = new PatternFill(PatternType.Solid, Color.FromArgb(255, 46, 204, 113), Colors.Transparent); - cell.SetFill(solidPatternFill); + var subItemCellFill = new PatternFill(PatternType.Solid, Color.FromArgb(255, 46, 204, 113), Colors.Transparent); + cell.SetFill(subItemCellFill); } } + + selection.Group(); + var originalItem = this.Items[parentItem.OriginalIndex]; + var isExpanded = (bool)originalItem.GetType().GetProperty("IsExpanded").GetValue(originalItem); + if (!isExpanded) + { + selection.SetHidden(true); + } } for (var j = 0; j < childGrid.Columns.Count; j++) diff --git a/GridView/ExportHierarchy/MainWindow.xaml b/GridView/ExportHierarchy/MainWindow.xaml index 3d765e9e..2a8df666 100644 --- a/GridView/ExportHierarchy/MainWindow.xaml +++ b/GridView/ExportHierarchy/MainWindow.xaml @@ -5,8 +5,8 @@ xmlns:my="clr-namespace:ExportHierarchy" Title="MainWindow" Height="700" Width="700"> - - + + @@ -33,6 +33,7 @@ - - + - - - + +