Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/11/12 17:44:26 (12 years ago)
Author:
abeham
Message:

#1754: Added copy & paste support for StringConvertibleArrayView similar to StringConvertibleMatrixView

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Data.Views/3.3/StringConvertibleArrayView.cs

    r7259 r7318  
    2323using System.ComponentModel;
    2424using System.Drawing;
     25using System.Text;
    2526using System.Windows.Forms;
    2627using HeuristicLab.Common;
     
    153154      dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
    154155    }
     156    private void dataGridView_KeyDown(object sender, KeyEventArgs e) {
     157      if (!ReadOnly && e.Control && e.KeyCode == Keys.V)
     158        PasteValuesToDataGridView();
     159      else if (e.Control && e.KeyCode == Keys.C)
     160        CopyValuesFromDataGridView();
     161      else if (e.Control && e.KeyCode == Keys.A)
     162        dataGridView.SelectAll();
     163    }
     164    private void CopyValuesFromDataGridView() {
     165      if (dataGridView.SelectedCells.Count == 0) return;
     166      StringBuilder s = new StringBuilder();
     167      int minRowIndex = dataGridView.SelectedCells[0].RowIndex;
     168      int maxRowIndex = dataGridView.SelectedCells[dataGridView.SelectedCells.Count - 1].RowIndex;
     169
     170      if (minRowIndex > maxRowIndex) {
     171        int temp = minRowIndex;
     172        minRowIndex = maxRowIndex;
     173        maxRowIndex = temp;
     174      }
     175
     176      for (int i = minRowIndex; i <= maxRowIndex; i++) {
     177        DataGridViewColumn column = dataGridView.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
     178        DataGridViewCell cell = dataGridView[column.Index, i];
     179        if (cell.Selected) {
     180          s.Append(Content.GetValue(i));
     181          s.Append(Environment.NewLine);
     182        }
     183      }
     184      Clipboard.SetText(s.ToString());
     185    }
     186    private void PasteValuesToDataGridView() {
     187      string[] values = SplitClipboardString(Clipboard.GetText());
     188      int rowIndex = 0;
     189      if (dataGridView.CurrentCell != null)
     190        rowIndex = dataGridView.CurrentCell.RowIndex;
     191
     192      if (Content.Length < rowIndex + values.Length) Content.Length = rowIndex + values.Length;
     193      for (int row = 0; row < values.Length; row++)
     194        Content.SetValue(values[row], row + rowIndex);
     195    }
     196    private string[] SplitClipboardString(string clipboardText) {
     197      clipboardText = clipboardText.Remove(clipboardText.Length - Environment.NewLine.Length);  //remove last newline constant
     198      return clipboardText.Split(new string[] { Environment.NewLine, "\t" }, StringSplitOptions.None);
     199    }
    155200    #endregion
    156201  }
Note: See TracChangeset for help on using the changeset viewer.