Example #1
0
        /// <summary>Create and return a datatable containing all rows and columns.</summary>
        public DataTable ToTable()
        {
            // Setup a table.
            var table = new DataTable(TableName);

            // Loop through all rows and add the values to the table.
            foreach (var row in Rows)
            {
                // Add a new row.
                var newRow = table.NewRow();

                // Add in all values for this row.
                for (int columnIndex = 0; columnIndex < ColumnNames.Count; columnIndex++)
                {
                    FlattenValueIntoRow(ColumnNames[columnIndex], ColumnUnits[columnIndex],
                                        row[columnIndex], newRow);
                }

                table.Rows.Add(newRow);
            }

            // Remove empty columns.
            for (int columnIndex = 0; columnIndex < table.Columns.Count; columnIndex++)
            {
                if (DataTableUtilities.ColumnIsNull(table.Columns[columnIndex]))
                {
                    table.Columns.RemoveAt(columnIndex);
                    // Need to decrement the column index because we have removed a column.
                    columnIndex--;
                }
            }

            return(table);
        }