Changeset 14443


Ignore:
Timestamp:
12/02/16 14:07:20 (4 years ago)
Author:
mkommend
Message:

#2714: Refactored key and selection events in DataGridView.

Location:
trunk/sources
Files:
2 edited

Legend:

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

    r14185 r14443  
    137137        for (int i = 0; i < columns.Length; ++i) {
    138138          var column = new DataGridViewTextBoxColumn();
     139          column.SortMode = DataGridViewColumnSortMode.Programmatic;
    139140          column.FillWeight = 1;
    140141          columns[i] = column;
  • trunk/sources/HeuristicLab.DataPreprocessing.Views/3.4/DataGridContentView.cs

    r14185 r14443  
    5858    public DataGridContentView() {
    5959      InitializeComponent();
     60      dataGridView.MouseDown += dataGridView_MouseDown;
    6061      dataGridView.CellMouseClick += dataGridView_CellMouseClick;
    6162      dataGridView.RowHeaderMouseClick += dataGridView_RowHeaderMouseClick;
    62       dataGridView.KeyDown += dataGridView_KeyDown;
    6363      dataGridView.MouseUp += dataGridView_MouseUp;
    6464      contextMenuCell.Items.Add(ShowHideColumns);
     
    204204    }
    205205
     206    //Necessary so that dataGridView.SelectedRows and SelectedColumns are populated correctly
     207    //further information: https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectedcolumns.aspx
     208    private void dataGridView_MouseDown(object sender, MouseEventArgs e) {
     209      var hitTestInfo = dataGridView.HitTest(e.X, e.Y);
     210      // row header click
     211      if (hitTestInfo.ColumnIndex == -1 && hitTestInfo.RowIndex >= 0) {
     212        if (dataGridView.SelectionMode != DataGridViewSelectionMode.RowHeaderSelect) {
     213          dataGridView.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
     214        }
     215      }
     216      // column header click
     217      if (hitTestInfo.RowIndex == -1 && hitTestInfo.ColumnIndex >= 0) {
     218        if (dataGridView.SelectionMode != DataGridViewSelectionMode.ColumnHeaderSelect) {
     219          dataGridView.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
     220        }
     221      }
     222    }
     223
    206224    protected override void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
    207       if (Content != null) {
    208         if (e.Button == MouseButtons.Left) {
    209           dataGridView.Focus();
    210           dataGridView.ClearSelection();
    211           dataGridView.SelectionChanged -= dataGridView_SelectionChanged;
    212           for (int i = 0; i < dataGridView.RowCount; i++) {
    213             if (i + 1 == dataGridView.RowCount)
    214               dataGridView.SelectionChanged += dataGridView_SelectionChanged;
    215             dataGridView[e.ColumnIndex, i].Selected = true;
    216           }
    217         } else if (e.Button == MouseButtons.Middle) {
    218           int newIndex = e.ColumnIndex >= 0 ? e.ColumnIndex : 0;
    219           Content.PreProcessingData.InsertColumn<double>(newIndex.ToString(), newIndex);
    220         } else if (e.Button == MouseButtons.Right && Content.SortableView) {
    221           SortColumn(e.ColumnIndex);
    222         }
    223       }
     225      if (Content == null) return;
     226
     227      if (e.Button == MouseButtons.Middle) {
     228        int newIndex = e.ColumnIndex >= 0 ? e.ColumnIndex : 0;
     229        Content.PreProcessingData.InsertColumn<double>(newIndex.ToString(), newIndex);
     230      } else if (e.Button == MouseButtons.Right && Content.SortableView) {
     231        SortColumn(e.ColumnIndex);
     232      }
     233
    224234      searchIterator = null;
    225235    }
     
    538548
    539549    protected override void dataGridView_KeyDown(object sender, KeyEventArgs e) {
    540       var selectedRows = dataGridView.SelectedRows;
    541       var selectedCells = dataGridView.SelectedCells;
    542       if (!Content.FilterLogic.IsFiltered) { //data is in read only mode....
    543         if (e.KeyCode == Keys.Delete && selectedCells.Count == Content.Rows && selectedCells.Count > 0) {
    544           Content.DeleteColumn(selectedCells[0].ColumnIndex);
    545         } else if (e.KeyCode == Keys.Delete && selectedRows.Count > 0) {
    546           List<int> rows = new List<int>();
    547           for (int i = 0; i < selectedRows.Count; ++i) {
    548             int index = (sortedColumnIndices.Count != 0) ? (Convert.ToInt32(selectedRows[i].HeaderCell.Value) - 1) :
    549               selectedRows[i].Index;
    550             rows.Add(index);
    551           }
    552           Content.DeleteRows(rows);
    553         } else if (e.Control && e.KeyCode == Keys.F) {
    554           CreateFindAndReplaceDialog();
    555           findAndReplaceDialog.ActivateSearch();
    556         } else if (e.Control && e.KeyCode == Keys.R) {
    557           CreateFindAndReplaceDialog();
    558           findAndReplaceDialog.ActivateReplace();
    559         }
     550      base.dataGridView_KeyDown(sender, e);
     551      //data is in read only mode....
     552      if (Content.FilterLogic.IsFiltered) return;
     553
     554      if (e.KeyCode == Keys.Delete) {
     555        //Delete column
     556        if (dataGridView.SelectedColumns.Count == 1) {
     557          Content.DeleteColumn(dataGridView.SelectedColumns[0].Index);
     558        }
     559        //Delete row
     560        if (dataGridView.SelectedRows.Count > 0) {
     561          //necessary if columns are sorted to translate the selected row index
     562          var rowIndexes = dataGridView.SelectedRows.Cast<DataGridViewRow>().Select(row => GetRowIndex(row.Index)).ToList();
     563          Content.DeleteRows(rowIndexes);
     564        }
     565      } else if (e.Control && e.KeyCode == Keys.F) {
     566        CreateFindAndReplaceDialog();
     567        findAndReplaceDialog.ActivateSearch();
     568      } else if (e.Control && e.KeyCode == Keys.R) {
     569        CreateFindAndReplaceDialog();
     570        findAndReplaceDialog.ActivateReplace();
    560571      }
    561572    }
Note: See TracChangeset for help on using the changeset viewer.