private void FillDefaultData(ExtendedDataGrid exGrid) { DataTable dataTable = new DataTable("DataTable" + this.NumberOfSheets); int rowsNum = 17; int colsNum = 13; for (int i = 0; i < colsNum; i++) { dataTable.Columns.Add(new DataColumn(SSColumns.ToString(i), typeof(SpreadsheetCell))); } for (int i = 0; i < rowsNum; i++) { var row = dataTable.NewRow(); dataTable.Rows.Add(row); for (int j = 0; j < colsNum; j++) { string cellName = SSColumns.ToString(j) + i; row[j] = new SpreadsheetCell(string.Empty, dataTable, new SpreadsheetCellCustomInfo(new CellName(cellName, i, j))); } } this.DataTables.Add(dataTable); exGrid.ItemsSource = dataTable.DefaultView; }
/// <summary> /// Resize table by creating new and copying old values /// </summary> /// <param name="exGrid"></param> /// <param name="oldDataTable"></param> /// <param name="rowsNum"></param> /// <param name="colsNum"></param> private void ResizeTable(ExtendedDataGrid exGrid, DataTable oldDataTable, int rowsNum, int colsNum) { int boundRows = oldDataTable.Rows.Count < rowsNum ? oldDataTable.Rows.Count : rowsNum; int boundCols = oldDataTable.Columns.Count < colsNum ? oldDataTable.Columns.Count : colsNum; DataTable newDataTable = new DataTable(oldDataTable.TableName); for (int i = 0; i < colsNum; i++) { newDataTable.Columns.Add(new DataColumn(SSColumns.ToString(i), typeof(SpreadsheetCell))); } for (int i = 0; i < rowsNum; i++) { var row = newDataTable.NewRow(); newDataTable.Rows.Add(row); for (int j = 0; j < colsNum; j++) { string cellName = SSColumns.ToString(j) + i; row[j] = new SpreadsheetCell(string.Empty, newDataTable, new SpreadsheetCellCustomInfo(new CellName(cellName, i, j))); } } // copy data for (int i = 0; i < boundRows; i++) { for (int j = 0; j < boundCols; j++) { SpreadsheetCell cell = newDataTable.GetSpreadsheetCell(i, j); SpreadsheetCell oldCell = oldDataTable.GetSpreadsheetCell(i, j); cell.Content = oldCell.Content; cell.Tag = oldCell.Tag; } } this.DataTables[this.CurrentSheetNumber] = newDataTable; // VERY VERY DIRTY HACK because nulling throws exception, dunno why try { exGrid.ItemsSource = null; } catch { } exGrid.ItemsSource = newDataTable.DefaultView; }