Free cookie consent management tool by TermsFeed Policy Generator

Changeset 15074


Ignore:
Timestamp:
06/27/17 17:39:34 (7 years ago)
Author:
mkommend
Message:

#2540: Changed StringConvertibleMatrixView to avoid CellValidating events of not existing cells.

File:
1 edited

Legend:

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

    r14443 r15074  
    131131      virtualRowIndices = Enumerable.Range(0, Content.Rows).ToArray();
    132132
    133       if (Content.Columns == 0 && dataGridView.ColumnCount != Content.Columns && !Content.ReadOnly)
    134         Content.Columns = dataGridView.ColumnCount;
    135       else {
    136         DataGridViewColumn[] columns = new DataGridViewColumn[Content.Columns];
    137         for (int i = 0; i < columns.Length; ++i) {
    138           var column = new DataGridViewTextBoxColumn();
    139           column.SortMode = DataGridViewColumnSortMode.Programmatic;
    140           column.FillWeight = 1;
    141           columns[i] = column;
    142         }
    143         dataGridView.Columns.Clear();
     133
     134      dataGridView.RowCount = 0;
     135
     136      DataGridViewColumn[] columns = new DataGridViewColumn[Content.Columns];
     137      for (int i = 0; i < columns.Length; ++i) {
     138        var column = new DataGridViewTextBoxColumn();
     139        column.SortMode = DataGridViewColumnSortMode.Programmatic;
     140        column.FillWeight = 1;
     141        columns[i] = column;
     142      }
     143      dataGridView.Columns.Clear();
     144
     145      if (Content.Columns != 0) {
    144146        dataGridView.Columns.AddRange(columns);
    145       }
    146 
    147       //DataGridViews with rows but no columns are not allowed !
    148       if (Content.Rows == 0 && dataGridView.RowCount != Content.Rows && !Content.ReadOnly)
    149         Content.Rows = dataGridView.RowCount;
    150       else
    151147        dataGridView.RowCount = Content.Rows;
    152 
     148      }
    153149
    154150      ClearSorting();
     
    263259
    264260    #region DataGridView Events
     261
    265262    protected virtual void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
    266       if (!dataGridView.ReadOnly) {
    267         string errorMessage;
    268         if (Content != null && !Content.Validate(e.FormattedValue.ToString(), out errorMessage)) {
    269           e.Cancel = true;
    270           dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
    271         }
    272       }
    273     }
     263      if (dataGridView.ReadOnly) return;
     264      if (Content == null) return;
     265      if (Content.Rows <= e.RowIndex || Content.Columns <= e.ColumnIndex) return;
     266
     267      string errorMessage;
     268      if (!Content.Validate(e.FormattedValue.ToString(), out errorMessage)) {
     269        e.Cancel = true;
     270        dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
     271      }
     272    }
     273
    274274    protected virtual void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
    275275      if (!dataGridView.ReadOnly) {
     
    441441      } else
    442442        if (newSortOrder != SortOrder.None)
    443           sortedColumnIndices.Add(new KeyValuePair<int, SortOrder>(columnIndex, newSortOrder));
     443        sortedColumnIndices.Add(new KeyValuePair<int, SortOrder>(columnIndex, newSortOrder));
    444444      Sort();
    445445    }
Note: See TracChangeset for help on using the changeset viewer.