/// <summary> /// Transforms a set of rows in a group into a crosstab. /// </summary> /// <param name="rows">Dictionary from crosstab value to the row index in the original ResultSet. /// Each of these rows has the same value for _groupByColumn. /// </param> /// <returns></returns> private Object[] PivotRow(Dictionary <RowKey, int> rows) { List <Object> values = new List <object>(); // First add the values that are common to all rows in the group. int firstRow = rows.Values.First(); foreach (ColumnInfo columnInfo in _normalColumns) { values.Add(_plainResultSet.GetValue(firstRow, columnInfo.ReportColumn)); } // Then, for each of the crosstab values, add the additional columns. foreach (RowKey pivotName in _pivotNames) { int?rowIndex = null; if (rows.ContainsKey(pivotName)) { rowIndex = rows[pivotName]; } foreach (ColumnInfo columnInfo in _pivotColumns) { Object value = null; if (rowIndex.HasValue) { value = _plainResultSet.GetValue(rowIndex.Value, columnInfo.ReportColumn); } values.Add(value); } } return(values.ToArray()); }
public Pivoter(PivotReport pivotReport, ResultSet plainResultSet) { _plainResultSet = plainResultSet; _normalColumns = new List <ColumnInfo>(); _pivotColumns = new List <ColumnInfo>(); IList <ReportColumn> groupByColumns = pivotReport.GroupByColumns; IList <ReportColumn> crosstabColumns = pivotReport.CrossTabHeaders; foreach (ReportColumn reportColumn in pivotReport.Columns) { _normalColumns.Add(plainResultSet.GetColumnInfo(reportColumn)); } foreach (ReportColumn reportColumn in pivotReport.CrossTabValues) { _pivotColumns.Add(plainResultSet.GetColumnInfo(reportColumn)); } Dictionary <RowKey, Dictionary <RowKey, int> > rowsById = new Dictionary <RowKey, Dictionary <RowKey, int> >(); ICollection <RowKey> pivotNameSet = new HashSet <RowKey>(); for (int i = 0; i < plainResultSet.RowCount; i++) { RowKey unqualifiedGroupByKey = new RowKey(); foreach (var reportColumn in groupByColumns) { unqualifiedGroupByKey.Add(plainResultSet.GetValue(i, reportColumn)); } RowKey crossTabKey = new RowKey(); foreach (var reportColumn in crosstabColumns) { crossTabKey.Add(plainResultSet.GetValue(i, reportColumn)); } // Add the data from the row into the spot for the GroupByKey and CrossTabKey. // In case that spot is already taken, an integer is appended to the GroupByKey // until a vacant spot to hold the data is found. Dictionary <RowKey, int> pivotNameDict; for (int iQualifier = 0; ; iQualifier++) { var qualifiedGroupByKey = new RowKey(); qualifiedGroupByKey.AddRange(unqualifiedGroupByKey); qualifiedGroupByKey.Add(iQualifier); if (!rowsById.TryGetValue(qualifiedGroupByKey, out pivotNameDict)) { pivotNameDict = new Dictionary <RowKey, int>(); rowsById.Add(qualifiedGroupByKey, pivotNameDict); } if (!pivotNameDict.ContainsKey(crossTabKey)) { break; } } pivotNameDict.Add(crossTabKey, i); pivotNameSet.Add(crossTabKey); } _pivotNames = new List <RowKey>(pivotNameSet); _pivotNames.Sort(); List <Object[]> rows = new List <object[]>(); foreach (Dictionary <RowKey, int> dict in rowsById.Values) { rows.Add(PivotRow(dict)); } _pivotedResultSet = new ResultSet(GetPivotedColumnInfos(), rows); }
public void SetResults(ResultSet resultSet) { dataGridView.Columns.Clear(); foreach (var columnInfo in resultSet.ColumnInfos) { if (columnInfo.IsHidden) continue; DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn { HeaderText = columnInfo.Caption, DefaultCellStyle = {Format = columnInfo.Format}, FillWeight = FILL_WEIGHT }; if (columnInfo.IsNumeric) { column.DefaultCellStyle.NullValue = TextUtil.EXCEL_NA; // Not L10N } dataGridView.Columns.Add(column); } for (int iRow = 0; iRow < resultSet.RowCount; iRow++ ) { var gridRow = dataGridView.Rows[dataGridView.Rows.Add()]; for (int iColumn = 0, iColumnGrid = 0; iColumn < resultSet.ColumnInfos.Count; iColumn++ ) { if (resultSet.ColumnInfos[iColumn].IsHidden) continue; gridRow.Cells[iColumnGrid++].Value = resultSet.GetValue(iRow, iColumn); } } }