Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/09/10 15:34:47 (14 years ago)
Author:
mkommend
Message:

adapted the ColumnsVisibilityDialog and corrected copy and past support in the StringConvertibleMatrixView (ticket #1134)

File:
1 edited

Legend:

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

    r4068 r4178  
    2424using System.ComponentModel;
    2525using System.Linq;
     26using System.Text;
    2627using System.Windows.Forms;
    2728using HeuristicLab.Common;
     
    103104      columnsTextBox.Text = Content.Columns.ToString();
    104105      columnsTextBox.Enabled = true;
    105       //DataGridViews with Rows but no columns are not allowed !
     106      //DataGridViews with rows but no columns are not allowed !
    106107      if (Content.Rows == 0 && dataGridView.RowCount != Content.Rows && !Content.ReadOnly)
    107108        Content.Rows = dataGridView.RowCount;
     
    116117      UpdateColumnHeaders();
    117118      dataGridView.Enabled = true;
     119      dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
    118120    }
    119121
    120122    private void UpdateColumnHeaders() {
    121       int firstDisplayedColumnIndex = this.dataGridView.FirstDisplayedScrollingColumnIndex;
    122       if (firstDisplayedColumnIndex == -1)
    123         firstDisplayedColumnIndex = 0;
    124       int lastDisplayedColumnIndex = firstDisplayedColumnIndex + dataGridView.DisplayedColumnCount(true);
    125       for (int i = firstDisplayedColumnIndex; i < lastDisplayedColumnIndex; i++) {
     123      for (int i = 0; i < dataGridView.ColumnCount; i++) {
    126124        if (Content.ColumnNames.Count() != 0)
    127125          dataGridView.Columns[i].HeaderText = Content.ColumnNames.ElementAt(i);
     
    137135        firstDisplayedRowIndex = 0;
    138136      int lastDisplaydRowIndex = firstDisplayedRowIndex + dataGridView.DisplayedRowCount(true);
     137
    139138      for (int i = firstDisplayedRowIndex; i < lastDisplaydRowIndex; i++) {
    140139        if (Content.RowNames.Count() != 0)
     
    143142          dataGridView.Rows[i].HeaderCell.Value = "Row " + (i + 1);
    144143      }
    145       dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
    146144    }
    147145
     
    245243      }
    246244    }
    247     private void dataGridView_Scroll(object sender, ScrollEventArgs e) {
    248       UpdateRowHeaders();
    249       UpdateColumnHeaders();
     245
     246    private void dataGridView_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e) {
     247      this.UpdateRowHeaders();
    250248    }
    251249    private void dataGridView_Resize(object sender, EventArgs e) {
    252       UpdateRowHeaders();
    253       UpdateColumnHeaders();
     250      this.UpdateRowHeaders();
    254251    }
    255252
    256253    private void dataGridView_KeyDown(object sender, KeyEventArgs e) {
    257       if (!ReadOnly && e.Control && e.KeyCode == Keys.V) { //shortcut for values paste
    258         string[,] values = SplitClipboardString(Clipboard.GetText());
    259 
    260         int rowIndex = 0;
    261         int columnIndex = 0;
    262         if (dataGridView.CurrentCell != null) {
    263           rowIndex = dataGridView.CurrentCell.RowIndex;
    264           columnIndex = dataGridView.CurrentCell.ColumnIndex;
    265         }
    266 
    267         for (int row = 0; row < values.GetLength(1); row++) {
    268           if (row + rowIndex >= Content.Rows)
    269             Content.Rows = Content.Rows + 1;
    270           for (int col = 0; col < values.GetLength(0); col++) {
    271             if (col + columnIndex >= Content.Columns)
    272               Content.Columns = Content.Columns + 1;
    273             Content.SetValue(values[col, row], row + rowIndex, col + columnIndex);
     254      if (!ReadOnly && e.Control && e.KeyCode == Keys.V)
     255        PasteValuesToDataGridView();
     256      else if (e.Control && e.KeyCode == Keys.C)
     257        CopyValuesFromDataGridView();
     258    }
     259
     260    private void CopyValuesFromDataGridView() {
     261      if (dataGridView.SelectedCells.Count == 0) return;
     262      StringBuilder s = new StringBuilder();
     263      int minRowIndex = dataGridView.SelectedCells[0].RowIndex;
     264      int maxRowIndex = dataGridView.SelectedCells[dataGridView.SelectedCells.Count - 1].RowIndex;
     265      int minColIndex = dataGridView.SelectedCells[0].ColumnIndex;
     266      int maxColIndex = dataGridView.SelectedCells[dataGridView.SelectedCells.Count - 1].ColumnIndex;
     267
     268      if (minRowIndex > maxRowIndex) {
     269        int temp = minRowIndex;
     270        minRowIndex = maxRowIndex;
     271        maxRowIndex = temp;
     272      }
     273      if (minColIndex > maxColIndex) {
     274        int temp = minColIndex;
     275        minColIndex = maxColIndex;
     276        maxColIndex = temp;
     277      }
     278
     279      bool addColumnNames = Content.ColumnNames.Any() && minRowIndex == 0;
     280      bool addRowNames = Content.RowNames.Any() && minColIndex == 0;
     281
     282      //add colum names
     283      if (addColumnNames) {
     284        if (addRowNames)
     285          s.Append('\t');
     286
     287        DataGridViewColumn column = dataGridView.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
     288        while (column != null) {
     289          s.Append(column.HeaderText);
     290          s.Append('\t');
     291          column = dataGridView.Columns.GetNextColumn(column, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
     292        }
     293        s.Remove(s.Length - 1, 1); //remove last tab
     294        s.Append(Environment.NewLine);
     295      }
     296
     297      for (int i = minRowIndex; i <= maxRowIndex; i++) {
     298        int rowIndex = this.virtualRowIndizes[i];
     299        if (addRowNames) {
     300          s.Append(Content.RowNames.ElementAt(rowIndex));
     301          s.Append('\t');
     302        }
     303
     304        DataGridViewColumn column = dataGridView.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
     305        while (column != null) {
     306          DataGridViewCell cell = dataGridView[column.Index, i];
     307          if (cell.Selected) {
     308            s.Append(Content.GetValue(rowIndex, column.Index));
    274309          }
    275         }
    276 
    277         ClearSorting();
    278       }
    279     }
    280 
     310          s.Append('\t');
     311          column = dataGridView.Columns.GetNextColumn(column, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
     312        }
     313        s.Remove(s.Length - 1, 1); //remove last tab
     314        s.Append(Environment.NewLine);
     315      }
     316      Clipboard.SetText(s.ToString());
     317    }
     318
     319    private void PasteValuesToDataGridView() {
     320      string[,] values = SplitClipboardString(Clipboard.GetText());
     321      int rowIndex = 0;
     322      int columnIndex = 0;
     323      if (dataGridView.CurrentCell != null) {
     324        rowIndex = dataGridView.CurrentCell.RowIndex;
     325        columnIndex = dataGridView.CurrentCell.ColumnIndex;
     326      }
     327
     328      for (int row = 0; row < values.GetLength(1); row++) {
     329        if (row + rowIndex >= Content.Rows)
     330          Content.Rows = Content.Rows + 1;
     331        for (int col = 0; col < values.GetLength(0); col++) {
     332          if (col + columnIndex >= Content.Columns)
     333            Content.Columns = Content.Columns + 1;
     334          Content.SetValue(values[col, row], row + rowIndex, col + columnIndex);
     335        }
     336      }
     337      ClearSorting();
     338    }
    281339    private string[,] SplitClipboardString(string clipboardText) {
    282340      clipboardText = clipboardText.Remove(clipboardText.Length - Environment.NewLine.Length);  //remove last newline constant
Note: See TracChangeset for help on using the changeset viewer.