private string GetClipString(CellRange rng, ClipboardCopyMode copyMode, bool selection) { if (copyMode == ClipboardCopyMode.None) { return string.Empty; } StringBuilder sb = new StringBuilder(); bool rowSelected = false; bool colSelected = false; if (selection) { int rowIndex = 0; while (rowIndex < Rows.Count) { if (!Rows[rowIndex].Selected) { rowIndex++; } else { rowSelected = true; break; } } int colIndex = 0; while (colIndex < Columns.Count) { if (!Columns[colIndex].Selected) { colIndex++; } else { colSelected = true; break; } } if (rowSelected || colSelected) { rng.SetRange(0, 0, Rows.Count - 1, Columns.Count - 1); colSelected = !rowSelected; } else if (SelectionMode == SelectionMode.Row || SelectionMode == SelectionMode.RowRange) { rng.Column = 0; rng.Column2 = Columns.Count - 1; } } if (copyMode == ClipboardCopyMode.IncludeColumnHeaders || copyMode == ClipboardCopyMode.IncludeAllHeaders) { for (int i = 0; i < ColumnHeaders.Rows.Count; i++) { if (copyMode == ClipboardCopyMode.IncludeAllHeaders) { for (int j = 0; j < TopLeftCells.Columns.Count; j++) { object formatted = TopLeftCells.GetDataFormatted(i, j); string topLeft = formatted != null ? formatted.ToString().Replace('\t', ' ') : string.Empty; sb.AppendFormat("{0}\t", topLeft); } } bool flag = true; for (int j = rng.LeftColumn; j <= rng.RightColumn; j++) { if (Columns[j].Visible && (!colSelected || Columns[j].Selected)) { if (!flag) { sb.Append('\t'); } flag = false; string formatted = ColumnHeaders.GetDataFormatted(i, j) as string; string colHeader = string.IsNullOrEmpty(formatted) ? Columns[j].GetHeader() : formatted; if (colHeader != null) { sb.Append(colHeader.Replace('\t', ' ')); } } } sb.AppendLine(); } } for (int i = rng.TopRow; i <= rng.BottomRow; i++) { if (Rows[i].Visible && (!rowSelected || Rows[i].Selected)) { if (copyMode == ClipboardCopyMode.IncludeRowHeaders || copyMode == ClipboardCopyMode.IncludeAllHeaders) { for (int j = 0; j < RowHeaders.Columns.Count; j++) { if (Columns[j].Visible && (!colSelected || Columns[j].Selected)) { object formatted = RowHeaders.GetDataFormatted(i, j); string rowHeader = formatted != null ? formatted.ToString().Replace('\t', ' ') : string.Empty; sb.AppendFormat("{0}\t", rowHeader); } } } bool flag = true; for (int j = rng.LeftColumn; j <= rng.RightColumn; j++) { if (Columns[j].Visible && (!colSelected || Columns[j].Selected)) { if (!flag) { sb.Append('\t'); } flag = false; string formatted = Cells.GetDataFormatted(i, j) as string; if (string.IsNullOrEmpty(formatted) && Rows[i] is GroupRow && Columns[j].Left == 0) { GroupRow groupRow = (GroupRow) Rows[i]; formatted = (string) GetGroupHeaderConverter().Convert(null, typeof (string), groupRow, GetCultureInfo()); } sb.Append(formatted != null ? formatted.Replace('\t', ' ') : string.Empty); } } sb.AppendLine(); } } return sb.ToString(); }
/// <summary> /// Parses a string into rows and columns and applies the content to a given range. /// </summary> /// <param name="text">Text to parse into the grid.</param> /// <param name="rng">Range where the text will be pasted.</param> /// <param name="copyMode"> /// Whether the string contains header information that should /// be discarded when applying the values to the cells. /// </param> /// <remarks> /// <para> /// The string contains rows delimited by newline characters and cells delimited /// by tabs (standard clipboard format). /// </para> /// <para> /// Only the top left cell of the <paramref name="rng" /> parameter is used; /// the number of rows and columns copied to the grid is determined by the content /// of the <paramref name="text" /> parameter. /// </para> /// </remarks> public void SetClipString(string text, CellRange rng, ClipboardCopyMode copyMode) { text = text.Replace("\r\n", "\r"); if (text.Length > 0 && text[text.Length - 1] == '\r') { text = text.Substring(0, text.Length - 1); } if (ClipboardPasteMode == ClipboardCopyMode.ExcludeHeader && !rng.IsSingleCell) { text = ExpandClipString(text, rng.RowSpan, rng.ColumnSpan); } int topRow = rng.TopRow; int leftColumn = rng.LeftColumn; rng = new CellRange(topRow, leftColumn); string[] lines = text.Split('\r', '\n'); int lineIndex = 0; int itemIndex = 0; switch (copyMode) { case ClipboardCopyMode.IncludeAllHeaders: { lineIndex = itemIndex = 1; break; } case ClipboardCopyMode.IncludeColumnHeaders: { lineIndex = 1; break; } case ClipboardCopyMode.IncludeRowHeaders: { itemIndex = 1; break; } } while (lineIndex < lines.Length && topRow < Rows.Count) { Row row = Rows[topRow]; rng.Row2 = topRow; if (row.Visible) { string line = lines[lineIndex]; string[] items = line.Split('\t'); leftColumn = rng.LeftColumn; while (itemIndex < items.Length && leftColumn < Columns.Count) { Column column = Columns[leftColumn]; rng.Column2 = Math.Max(rng.Column2, leftColumn); if (!column.Visible) { itemIndex--; } else if (!IsReadOnly && !row.IsReadOnly && !column.IsReadOnly) { CellRange cellRange = new CellRange(topRow, leftColumn); CellEditEventArgs cellEditEventArg = new CellEditEventArgs(Cells, cellRange, null, false); OnBeginningEdit(cellEditEventArg); if (!cellEditEventArg.Cancel) { object item = items[itemIndex]; try { this[topRow, leftColumn] = item; UpdateAggregates(column, false); } catch { } OnCellEditEnded(cellEditEventArg); } } itemIndex++; leftColumn++; } } else { lineIndex--; } lineIndex++; topRow++; } Selection = rng; for (int i = rng.LeftColumn; i <= rng.RightColumn; i++) { UpdateAggregates(Columns[i], false); } }
/// <summary> /// Gets the current selection as a string suitable for copying to the clipboard. /// </summary> /// <param name="copyMode"> /// <see cref="P:C1.WPF.FlexGrid.C1FlexGrid.ClipboardCopyMode" /> that specifies which /// headers should be included in the string. /// </param> /// <returns> /// A string with the content of the current selection. Columns are delimited /// by tabs and rows are delimited by new lines. /// </returns> public string GetClipString(ClipboardCopyMode copyMode) { return GetClipString(Selection, copyMode, true); }
/// <summary> /// Gets a range of cells as a string suitable for copying to the clipboard. /// </summary> /// <param name="rng"> /// <see cref="T:C1.WPF.FlexGrid.CellRange" /> that specifies the range of cells that /// should be included in the string. /// </param> /// <param name="copyMode"> /// <see cref="P:C1.WPF.FlexGrid.C1FlexGrid.ClipboardCopyMode" /> that specifies which /// headers should be included in the string. /// </param> /// <returns> /// A string with the content of the current selection. Columns are delimited /// by tabs and rows are delimited by new lines. /// </returns> public string GetClipString(CellRange rng, ClipboardCopyMode copyMode) { return GetClipString(rng, copyMode, false); }
void SetPropertyToGrids(ClipboardCopyMode copyMode) { firstGrid.ClipboardCopyMode = copyMode; secondGrid.ClipboardCopyMode = copyMode; }