From bfb9e3c19e57da684e9406c3298c61240cf48f9f Mon Sep 17 00:00:00 2001 From: John Date: Tue, 3 Apr 2012 14:23:34 +0000 Subject: [PATCH] Was sometimes getting an error message during the Paste operation of a table Row or Column. B2012-085 and B2012-086 Fixed some other Copy/Paste Rows/Columns table bugs as well. --- PROMS/Volian.Controls.Library/VlnFlexGrid.cs | 193 ++++++++++++------- 1 file changed, 121 insertions(+), 72 deletions(-) diff --git a/PROMS/Volian.Controls.Library/VlnFlexGrid.cs b/PROMS/Volian.Controls.Library/VlnFlexGrid.cs index 8e471edb..6307fc06 100644 --- a/PROMS/Volian.Controls.Library/VlnFlexGrid.cs +++ b/PROMS/Volian.Controls.Library/VlnFlexGrid.cs @@ -2311,8 +2311,12 @@ namespace Volian.Controls.Library //foreach (CellRange crng in this.MergedRanges) // Console.WriteLine("merge ranges [{0},{1}] - [{2},{3}]", crng.r1, crng.c1, crng.r2, crng.c2); } + private void AdjustMergedRows(int row, bool above, bool removing) + { + AdjustMergedRows(row, 1, above, removing); + } - private void AdjustMergedRows(int row, bool above, bool removing) + private void AdjustMergedRows(int row, int cnt, bool above, bool removing) { CellRangeCollection crc = new CellRangeCollection(this); if (removing) @@ -2324,7 +2328,7 @@ namespace Volian.Controls.Library { if (r.TopRow != r.BottomRow) { - cr.r2--; + cr.r2 -= cnt; crc.Add(cr); } } @@ -2333,8 +2337,8 @@ namespace Volian.Controls.Library if (row <= r.r1) { if (row < r.r1) - cr.r1--; - cr.r2--; + cr.r1 -= cnt; + cr.r2 -= cnt; } crc.Add(cr); } @@ -2347,34 +2351,40 @@ namespace Volian.Controls.Library int inspos = (above) ? row : row - 1; if (r.ContainsRow(inspos)) { - if ((above && cr.r1 == inspos) || (!above && cr.r2 == inspos)) + bool adjustMergeValues = true; + if (cr.r1 == row && cr.r2 == row + cnt -1) + adjustMergeValues = false; // don't add to existing merge range - we will add a new merge range for this copy + for (int insCnt = 0; insCnt < cnt; insCnt++) { - string tstr = ""; - int newrow = 0; - if (above) + if ((above && cr.r1 == inspos) || (!above && cr.r2 == inspos)) { - if (this[cr.r1 + 1, cr.c1] != null) - tstr = this[cr.r1 + 1, cr.c1].ToString(); - newrow = cr.r1; + string tstr = ""; + int newrow = 0; + if (above) + { + if (this[cr.r1 + cnt, cr.c1] != null) + tstr = this[cr.r1 + cnt, cr.c1].ToString(); + newrow = cr.r1; + } + else + { + if (this[cr.r2, cr.c1] != null) + tstr = this[cr.r2, cr.c1].ToString(); + newrow = cr.r2 + cnt; + } + if (tstr != null && tstr.Length > 0) + for (int x = cr.c1; x <= cr.c2; x++) + this[newrow, x] = tstr; } - else - { - if (this[cr.r2, cr.c1] != null) - tstr = this[cr.r2, cr.c1].ToString(); - newrow = cr.r2 + 1; - } - if (tstr != null && tstr.Length > 0) - for (int x = cr.c1; x <= cr.c2; x++) - this[newrow, x] = tstr; + if (adjustMergeValues) cr.r2++; } - cr.r2++; } else { if (inspos < cr.r1) { - cr.r1++; - cr.r2++; + cr.r1 += cnt; + cr.r2 += cnt; } } crc.Add(cr); @@ -2387,7 +2397,12 @@ namespace Volian.Controls.Library } } - private void AdjustMergedColumns(int col, bool left, bool removing) + private void AdjustMergedColumns(int col, bool left, bool removing) + { + AdjustMergedColumns(col, 1, left, removing); //adjusting for just one column + } + + private void AdjustMergedColumns(int col, int cnt, bool left, bool removing) { CellRangeCollection crc = new CellRangeCollection(this); if (removing) @@ -2399,7 +2414,7 @@ namespace Volian.Controls.Library { if (r.LeftCol != r.RightCol) { - cr.c2--; + cr.c2 -= cnt; crc.Add(cr); } } @@ -2407,9 +2422,8 @@ namespace Volian.Controls.Library { if (col < cr.c1) { - cr.c1--; - cr.c2--; - } + cr.c1-=cnt; + cr.c2-=cnt; } crc.Add(cr); } } @@ -2421,31 +2435,34 @@ namespace Volian.Controls.Library int inspos = (left) ? col : col - 1; if (r.ContainsCol(inspos)) { - string tstr = ""; - int newcol = 0; - if (left) + for (int insCnt = 0; insCnt < cnt; insCnt++) { - if (inspos == cr.c1) - tstr = (this[cr.r1, cr.c1 + 1] == null)?"":this[cr.r1, cr.c1 + 1].ToString(); + string tstr = ""; + int newcol = 0; + if (left) + { + if (inspos == cr.c1) + tstr = (this[cr.r1, cr.c1 + 1] == null) ? "" : this[cr.r1, cr.c1 + 1].ToString(); + else + tstr = (this[cr.r1, cr.c1] == null) ? "" : this[cr.r1, cr.c1].ToString(); + newcol = cr.c1; + } else - tstr = (this[cr.r1, cr.c1] == null) ? "" : this[cr.r1, cr.c1].ToString(); - newcol = cr.c1; - } - else - { - if (this[cr.r1, cr.c2] != null) - tstr = this[cr.r1, cr.c2].ToString(); - newcol = cr.c2 + 1; - } - for (int x = cr.r1; x <= cr.r2; x++) - this[x, newcol] = tstr; + { + if (this[cr.r1, cr.c2] != null) + tstr = this[cr.r1, cr.c2].ToString(); + newcol = cr.c2 + 1; + } + for (int x = cr.r1; x <= cr.r2; x++) + this[x, newcol] = tstr; - cr.c2++; + cr.c2++; + } } else if (col <= r.c1) { - cr.c1++; - cr.c2++; + cr.c1+=cnt; + cr.c2+=cnt; } crc.Add(cr); } @@ -2814,45 +2831,46 @@ namespace Volian.Controls.Library if (MyCopyInfo.MyCopiedFlexGrid == null || MyCopyInfo.MyCopyOption != GridCopyOption.Row) return; int rowsToAdd = MyCopyInfo.MyCopiedCellRange.r2 - MyCopyInfo.MyCopiedCellRange.r1 + 1; int colsToAdd = Math.Max((MyCopyInfo.MyCopiedCellRange.c2 - MyCopyInfo.MyCopiedCellRange.c1 + 1) - Cols.Count, 0); - int rowOffset = Selection.r1 + (pp == enmPastePos.After ? 1 : 0) - MyCopyInfo.MyCopiedCellRange.r1; int selR1 = Selection.r1; + int selR2 = Selection.r2; + int rowOffset = selR1 + (pp == enmPastePos.After ?(selR2-selR1)+1 : 0) - MyCopyInfo.MyCopiedCellRange.r1; // If the rows we are copying has more columns than the current table, then append then needed columns to the grid if (colsToAdd > 0) { Cols.Add(colsToAdd); - AdjustMergedColumns(Cols.Count - 1, false, false); + AdjustMergedColumns(Cols.Count - 1, colsToAdd, false, false); MyBorders.InsertColumns(Cols.Count - colsToAdd - 1, colsToAdd); } switch (pp) { case enmPastePos.Before: - //this.Rows.InsertRange(Selection.r1,rowsToAdd); this.Rows.InsertRange(selR1, rowsToAdd); - AdjustMergedRows(selR1, true, false); + AdjustMergedRows(selR1, rowsToAdd, true, false); + MyBorders.InsertRows(Rows.Count - rowsToAdd - 1, rowsToAdd); break; case enmPastePos.After: - if (selR1 < Rows.Count - 1) + if (selR2+1 < Rows.Count - 1) { - //this.Rows.InsertRange(Selection.r1 + 1, rowsToAdd); - this.Rows.InsertRange(selR1 + 1, rowsToAdd); + this.Rows.InsertRange(selR2 + 1, rowsToAdd); + AdjustMergedRows(selR1+1, rowsToAdd, false, false); } else this.Rows.Add(rowsToAdd); - AdjustMergedRows(selR1 + 1, false, false); + MyBorders.InsertRows(Rows.Count - rowsToAdd - 1, rowsToAdd); break; case enmPastePos.Replace: if (rowsToAdd > Rows.Count - Selection.r1) { - this.Rows.Add(rowsToAdd - (Rows.Count - Selection.r1)); - AdjustMergedRows(selR1, false, false); + int numToAdd = rowsToAdd - (Rows.Count = selR1); + this.Rows.Add(numToAdd); + AdjustMergedRows(selR1, numToAdd, false, false); } break; } RemoveMergedRanges(MyCopyInfo.MyCopiedCellRange.r1 + rowOffset, MyCopyInfo.MyCopiedCellRange.c1, MyCopyInfo.MyCopiedCellRange.r2 + rowOffset, MyCopyInfo.MyCopiedCellRange.c2); - for (int r = MyCopyInfo.MyCopiedCellRange.r1; r <= MyCopyInfo.MyCopiedCellRange.r2; r++) { Rows[r + rowOffset].Height = MyCopyInfo.MyCopiedFlexGrid.Rows[r].Height; @@ -2863,15 +2881,26 @@ namespace Volian.Controls.Library { Cols[c].Width = Math.Max(Cols[c].Width, MyCopyInfo.MyCopiedFlexGrid.Cols[c].Width); CellRange cr = GetCellRange(crm.r1 + rowOffset, crm.c1, crm.r2 + rowOffset, crm.c2); + // if the paste is above - copy merge range for the destination + // if the paste is below - put back the range for the source location if ((crm.r2 > crm.r1) || (crm.c2 > crm.c1)) MergedRanges.Add(cr); this[r + rowOffset, c] = MyCopyInfo.MyCopiedFlexGrid[r, c]; MatchStyle(crm, cr); PasteBorders(r + rowOffset, c, r, c); + // if the paste is above - put back the range for the source location + // if the paste is below - copy merge range for the destination + int r1 = crm.r1 + rowsToAdd - rowOffset; + int r2 = crm.r2 + rowsToAdd - rowOffset; + CellRange ncr = GetCellRange(r1, crm.c1, r2, crm.c2); + if ((crm.r2 > crm.r1) || (crm.c2 > crm.c1)) + MergedRanges.Add(ncr); } } } + this.AdjustGridControlSize(); + CopyToCopiedFlexGrid(MyCopyInfo.MyCopyOption); } private void MatchStyle(CellRange crm, CellRange cr) @@ -2904,8 +2933,9 @@ namespace Volian.Controls.Library int colsToAdd = MyCopyInfo.MyCopiedCellRange.c2 - MyCopyInfo.MyCopiedCellRange.c1 + 1; int rowsToAdd = Math.Max((MyCopyInfo.MyCopiedCellRange.r2 - MyCopyInfo.MyCopiedCellRange.r1 + 1) - Rows.Count, 0); - int colOffset = Selection.c1 + (pp == enmPastePos.After ? 1 : 0) - MyCopyInfo.MyCopiedCellRange.c1; int selC1 = Selection.c1; + int selC2 = Selection.c2; + int colOffset = selC1 + (pp == enmPastePos.After ? (selC2-selC1)+1 : 0) - MyCopyInfo.MyCopiedCellRange.c1; // If the columns we are copying has more rows than the current table, then append the needed rows to the grid if (rowsToAdd > 0) @@ -2918,32 +2948,33 @@ namespace Volian.Controls.Library switch (pp) { case enmPastePos.Before: - //this.Cols.InsertRange(Selection.c1, colsToAdd); this.Cols.InsertRange(selC1, colsToAdd); - AdjustMergedColumns(selC1, true, false); + AdjustMergedColumns(selC1, colsToAdd, true, false); + MyBorders.InsertColumns(Cols.Count - colsToAdd - 1, colsToAdd); break; case enmPastePos.After: - //if (Selection.c1 < Cols.Count - 1) - // this.Cols.InsertRange(Selection.c1 + 1, colsToAdd); - if (selC1 < Cols.Count - 1) - this.Cols.InsertRange(selC1 + 1, colsToAdd); + if (selC2+1 < Cols.Count - 1) + { + this.Cols.InsertRange(selC2+1, colsToAdd); + AdjustMergedColumns(selC1+1, colsToAdd, false, false); + } else this.Cols.Add(colsToAdd); - AdjustMergedColumns(selC1 + 1, false, false); + MyBorders.InsertColumns(Cols.Count - colsToAdd - 1, colsToAdd); break; case enmPastePos.Replace: - //if (colsToAdd > Cols.Count - Selection.c1) - // this.Cols.Add(colsToAdd - (Cols.Count - Selection.c1)); if (colsToAdd > Cols.Count - selC1) { - this.Cols.Add(colsToAdd - (Cols.Count - selC1)); - AdjustMergedColumns(selC1, true, false); + int numToAdd = colsToAdd - (Cols.Count - selC1); + this.Cols.Add(numToAdd); + AdjustMergedColumns(selC1, numToAdd, true, false); } break; } RemoveMergedRanges(MyCopyInfo.MyCopiedCellRange.r1, MyCopyInfo.MyCopiedCellRange.c1 + colOffset, MyCopyInfo.MyCopiedCellRange.r2, MyCopyInfo.MyCopiedCellRange.c2 + colOffset); + // Fix the merge ranges for (int c = MyCopyInfo.MyCopiedCellRange.c1; c <= MyCopyInfo.MyCopiedCellRange.c2; c++) { Cols[c + colOffset].Width = MyCopyInfo.MyCopiedFlexGrid.Cols[c].Width; @@ -2954,15 +2985,28 @@ namespace Volian.Controls.Library { Rows[r].Height = Math.Max(Rows[r].Height, MyCopyInfo.MyCopiedFlexGrid.Rows[r].Height); CellRange cr = GetCellRange(crm.r1, crm.c1 + colOffset, crm.r2, crm.c2 + colOffset); + // if the paste is to the left - copy merge range for the destination + // if the paste is to the right - put back the range for the source location if ((crm.r2 > crm.r1) || (crm.c2 > crm.c1)) MergedRanges.Add(cr); + // copy cell text to the destination, copy the style, copy the cell borders this[r, c + colOffset] = MyCopyInfo.MyCopiedFlexGrid[r, c]; MatchStyle(crm, cr); PasteBorders(r, c + colOffset, r, c); + // if the paste is to the left - put back the range for the source location + // if the paste is to the right - copy merge range for the destination + int c1 = crm.c1 + colsToAdd - colOffset; + int c2 = crm.c2 + colsToAdd - colOffset; + CellRange ncr = GetCellRange(crm.r1, c1, crm.r2, c2); + if ((crm.r2 > crm.r1) || (crm.c2 > crm.c1)) + MergedRanges.Add(ncr); } } } + this.AdjustGridControlSize(); + CopyToCopiedFlexGrid(MyCopyInfo.MyCopyOption); + } public void PasteCopiedCells() @@ -2986,21 +3030,25 @@ namespace Volian.Controls.Library if (rowsToAdd > 0) { if (selR2 < Rows.Count - 1) + { this.Rows.InsertRange(selR2 + 1, rowsToAdd); + AdjustMergedRows(selR2 + 1, false, false); + } else this.Rows.Add(rowsToAdd); MyBorders.InsertRows(selR2,rowsToAdd); - AdjustMergedRows(selR2 + 1, false, false); } if (colsToAdd > 0) { if (selC2 < Cols.Count - 1) + { this.Cols.InsertRange(selC2 + 1, colsToAdd); + AdjustMergedColumns(selC2 + 1, false, false); + } else this.Cols.Add(colsToAdd); MyBorders.InsertColumns(selC2,colsToAdd); - AdjustMergedColumns(selC2 + 1, false, false); } RemoveMergedRanges(MyCopyInfo.MyCopiedCellRange.r1 + rowOffset, MyCopyInfo.MyCopiedCellRange.c1 + colOffset, MyCopyInfo.MyCopiedCellRange.r2 + rowOffset, MyCopyInfo.MyCopiedCellRange.c2 + colOffset); @@ -3026,6 +3074,7 @@ namespace Volian.Controls.Library } } this.AdjustGridControlSize(); + CopyToCopiedFlexGrid(MyCopyInfo.MyCopyOption); } #endregion // Copy/Paste