public IEnumerator <TableExporter.ITableRow> GetEnumerator() { IEnumerable exportedRows; IList <int> exportedColumns; if (exportSelectedCellsOnly && treeDataGridView.SelectedCells.Count > 0 && !treeDataGridView.AreAllCellsSelected(false)) { var selectedRows = new Set <DataGridViewRow>(); var selectedColumns = new Map <int, int>(); // ordered by DisplayIndex foreach (DataGridViewCell cell in treeDataGridView.SelectedCells) { selectedRows.Add(cell.OwningRow); selectedColumns[cell.OwningColumn.DisplayIndex] = cell.ColumnIndex; } exportedRows = selectedRows; exportedColumns = selectedColumns.Values; } else { exportedRows = treeDataGridView.Rows; exportedColumns = treeDataGridView.GetVisibleColumnsInDisplayOrder().Select(o => o.Index).ToList(); } // add column headers var Headers = new List <string>(); foreach (var columnIndex in exportedColumns) { Headers.Add(treeDataGridView.Columns[columnIndex].HeaderText); } foreach (DataGridViewRow row in exportedRows) { var exportedRow = new ExportedTableRow(); exportedRow.Headers = Headers; /* TODO: how to handle non-root rows? * var row = rows[rowIndex]; * * // skip non-root rows or filtered rows * if (rowIndexHierarchy.Count > 1 || getRowFilterState(row) == RowFilterState.Out) * continue;*/ if (rows.Count > row.Index) { Row row2 = rows[row.Index]; if (getRowFilterState(row2) == RowFilterState.Out) { continue; } } var rowIndexHierarchy = treeDataGridView.GetRowHierarchyForRowIndex(row.Index); exportedRow.Cells = new List <string>(); foreach (var columnIndex in exportedColumns) { // empty cells are exported as blank except for pivot columns which are exported as zeros object value = row.Cells[columnIndex].Value ?? (treeDataGridView.Columns[columnIndex].Name.StartsWith("pivot") ? "0" : String.Empty); var sb = new StringBuilder(value.ToString()); if (columnIndex == 0 && rowIndexHierarchy.Count > 1) { int indent = (rowIndexHierarchy.Count - 1) * 2; sb.Insert(0, new string(' ', indent)); } exportedRow.Cells.Add(sb.ToString()); } yield return(exportedRow); } }
public IEnumerator<TableExporter.ITableRow> GetEnumerator() { IEnumerable exportedRows; IList<int> exportedColumns; if (exportSelectedCellsOnly && treeDataGridView.SelectedCells.Count > 0 && !treeDataGridView.AreAllCellsSelected(false)) { var selectedRows = new Set<DataGridViewRow>(); var selectedColumns = new Map<int, int>(); // ordered by DisplayIndex foreach (DataGridViewCell cell in treeDataGridView.SelectedCells) { selectedRows.Add(cell.OwningRow); selectedColumns[cell.OwningColumn.DisplayIndex] = cell.ColumnIndex; } exportedRows = selectedRows; exportedColumns = selectedColumns.Values; } else { exportedRows = treeDataGridView.Rows; exportedColumns = treeDataGridView.GetVisibleColumnsInDisplayOrder().Select(o => o.Index).ToList(); } // add column headers var Headers = new List<string>(); foreach (var columnIndex in exportedColumns) Headers.Add(treeDataGridView.Columns[columnIndex].HeaderText); foreach (DataGridViewRow row in exportedRows) { var exportedRow = new ExportedTableRow(); exportedRow.Headers = Headers; /* TODO: how to handle non-root rows? var row = rows[rowIndex]; // skip non-root rows or filtered rows if (rowIndexHierarchy.Count > 1 || getRowFilterState(row) == RowFilterState.Out) continue;*/ if (rows.Count > row.Index) { Row row2 = rows[row.Index]; if (getRowFilterState(row2) == RowFilterState.Out) continue; } var rowIndexHierarchy = treeDataGridView.GetRowHierarchyForRowIndex(row.Index); exportedRow.Cells = new List<string>(); foreach (var columnIndex in exportedColumns) { // empty cells are exported as blank except for pivot columns which are exported as zeros object value = row.Cells[columnIndex].Value ?? (treeDataGridView.Columns[columnIndex].Name.StartsWith("pivot") ? "0" : String.Empty); var sb = new StringBuilder(value.ToString()); if (columnIndex == 0 && rowIndexHierarchy.Count > 1) { int indent = (rowIndexHierarchy.Count - 1) * 2; sb.Insert(0, new string(' ', indent)); } exportedRow.Cells.Add(sb.ToString()); } yield return exportedRow; } }