public void AddColumn(dynamic[] newCol = null, string headerName = "") { if (newCol.Length > NumberOfRows) { throw new Exception("Number of rows in the new column (" + newCol.Length + ") must be less than or equal to the number of rows in the dataset{" + NumberOfRows + ")"); } dynamic[] dataToAdd = new dynamic[NumberOfRows]; for (int r = 0; r < newCol.Length; r++) { dataToAdd[r] = newCol[r]; } dynamic[,] newData = new dynamic[NumberOfRows, NumberOfColumns + 1]; string[] newHeaders = new string[NumberOfColumns + 1]; for (int row = 0; row < NumberOfRows; row++) { for (int col = 0; col < NumberOfColumns; col++) { newData[row, col] = _data[row, col]; } } for (int col = 0; col < NumberOfColumns; col++) { newHeaders[col] = _headers[col]; } newHeaders[NumberOfColumns] = (string.IsNullOrWhiteSpace(headerName))? NumberOfColumns.ToString() : headerName; for (int row = 0; row < NumberOfRows; row++) { newData[row, NumberOfColumns] = dataToAdd[row]; } _data = newData; _headers = newHeaders; NumberOfColumns++; }
public string ToString(int numberOfRows = 5, int colWidth = 10, int maxRows = 300, bool?printRowLabels = null, bool printDataTypes = false) { string output = ""; bool printRowLabs = (printRowLabels == null)? false : true; printRowLabs = (printRowLabs | LabeledRows)? true:false; numberOfRows = (numberOfRows > maxRows)? maxRows : numberOfRows; var row1 = (NumberOfColumns * colWidth); var row2 = (Convert.ToInt32(printRowLabs) * colWidth); int rowWidth = (NumberOfColumns * colWidth) + (Convert.ToInt32(printRowLabs) * colWidth); output += (printRowLabs)? ColValue(NumberOfRows.ToString() + "x" + NumberOfColumns.ToString()) + '|' : ""; foreach (string h in _headers) { output += ColValue(h) + '|'; } output += Environment.NewLine; if (printDataTypes) { output += (printRowLabs)? ColValue("...") + '|' : ""; foreach (Type t in _columnDataTypes) { output += ColValue(t.Name, '-') + '|'; } output += Environment.NewLine; } output += new String('-', rowWidth - (rowWidth / 10)).ToString(); for (int r = 0; r < numberOfRows; r++) { output += Environment.NewLine; output += MakeRow(r); } string MakeRow(int row) { string outRow = ""; if (printRowLabs) { outRow += ColValue(_rowNames[row].ToString()) + '|'; } for (int col = 0; col < NumberOfColumns; col++) { outRow += ColValue(_data[row, col].ToString()) + '|'; } return(outRow); } string ColValue(string value, char fill = ' ') { int valLength = value.Length; string valueToWrite = ""; if (valLength > colWidth - 4) { valueToWrite = value.Substring(0, colWidth - 4); } else { if (valLength % 2 == 0) { int numspaces = ((colWidth - 4) - valLength) / 2; valueToWrite = (new String(fill, numspaces)) + value + (new String(fill, numspaces)); } else { int numspaces = ((colWidth - 4) - valLength - 1) / 2; valueToWrite = (new String(' ', numspaces)) + value + (new String(' ', numspaces)) + " "; } } return(" " + valueToWrite + " "); } return(output); }