Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ImprovingStringConvertibleMatrix/HeuristicLab.Data.Views/3.3/StringConvertibleMatrixView.cs @ 9307

Last change on this file since 9307 was 9307, checked in by sforsten, 11 years ago

#2018: restored original behaviour of StringConvertibleMatrixView

File size: 23.2 KB
RevLine 
[2669]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2669]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
[4068]23using System.Collections.Generic;
[2669]24using System.ComponentModel;
[4652]25using System.Drawing;
[3312]26using System.Linq;
[4178]27using System.Text;
[2669]28using System.Windows.Forms;
29using HeuristicLab.Common;
30using HeuristicLab.MainForm;
[2713]31using HeuristicLab.MainForm.WindowsForms;
[2669]32
33namespace HeuristicLab.Data.Views {
[3048]34  [View("StringConvertibleMatrix View")]
35  [Content(typeof(IStringConvertibleMatrix), true)]
[3228]36  public partial class StringConvertibleMatrixView : AsynchronousContentView {
[8833]37    protected int[] virtualRowIndices;
[8139]38    private List<KeyValuePair<int, SortOrder>> sortedColumnIndices;
[3430]39    private RowComparer rowComparer;
40
[3048]41    public new IStringConvertibleMatrix Content {
42      get { return (IStringConvertibleMatrix)base.Content; }
[2713]43      set { base.Content = value; }
[2669]44    }
45
[6680]46    public DataGridView DataGridView {
47      get { return dataGridView; }
48    }
49
[3430]50    public override bool ReadOnly {
51      get {
52        if ((Content != null) && Content.ReadOnly) return true;
53        return base.ReadOnly;
54      }
55      set { base.ReadOnly = value; }
56    }
[3314]57
[4652]58    private bool showRowsAndColumnsTextBox;
59    public bool ShowRowsAndColumnsTextBox {
60      get { return showRowsAndColumnsTextBox; }
61      set {
[4709]62        showRowsAndColumnsTextBox = value;
63        UpdateVisibilityOfTextBoxes();
[4652]64      }
65    }
66
[4709]67    private bool showStatisticalInformation;
68    public bool ShowStatisticalInformation {
69      get { return showStatisticalInformation; }
70      set {
71        showStatisticalInformation = value;
72        UpdateVisibilityOfStatisticalInformation();
73      }
74    }
75
[3048]76    public StringConvertibleMatrixView() {
[2669]77      InitializeComponent();
[4709]78      ShowRowsAndColumnsTextBox = true;
[4720]79      ShowStatisticalInformation = true;
[2677]80      errorProvider.SetIconAlignment(rowsTextBox, ErrorIconAlignment.MiddleLeft);
81      errorProvider.SetIconPadding(rowsTextBox, 2);
82      errorProvider.SetIconAlignment(columnsTextBox, ErrorIconAlignment.MiddleLeft);
83      errorProvider.SetIconPadding(columnsTextBox, 2);
[8139]84      sortedColumnIndices = new List<KeyValuePair<int, SortOrder>>();
[3314]85      rowComparer = new RowComparer();
[2669]86    }
87
[2713]88    protected override void DeregisterContentEvents() {
[9306]89      Content.ItemsChanged -= new EventHandler<MatrixValuesChangedEventArgs>(Content_ItemChanged);
[2713]90      Content.Reset -= new EventHandler(Content_Reset);
[3320]91      Content.ColumnNamesChanged -= new EventHandler(Content_ColumnNamesChanged);
92      Content.RowNamesChanged -= new EventHandler(Content_RowNamesChanged);
[2713]93      base.DeregisterContentEvents();
[2669]94    }
[2713]95    protected override void RegisterContentEvents() {
96      base.RegisterContentEvents();
[9306]97      Content.ItemsChanged += new EventHandler<MatrixValuesChangedEventArgs>(Content_ItemChanged);
[2713]98      Content.Reset += new EventHandler(Content_Reset);
[3320]99      Content.ColumnNamesChanged += new EventHandler(Content_ColumnNamesChanged);
100      Content.RowNamesChanged += new EventHandler(Content_RowNamesChanged);
[2669]101    }
102
[2713]103    protected override void OnContentChanged() {
104      base.OnContentChanged();
105      if (Content == null) {
[2677]106        rowsTextBox.Text = "";
107        columnsTextBox.Text = "";
[2669]108        dataGridView.Rows.Clear();
[2677]109        dataGridView.Columns.Clear();
[8139]110        virtualRowIndices = new int[0];
[3936]111      } else
[2713]112        UpdateData();
[3904]113    }
[3764]114
[3904]115    protected override void SetEnabledStateOfControls() {
116      base.SetEnabledStateOfControls();
[3454]117      rowsTextBox.Enabled = Content != null;
118      columnsTextBox.Enabled = Content != null;
119      dataGridView.Enabled = Content != null;
120      rowsTextBox.ReadOnly = ReadOnly;
121      columnsTextBox.ReadOnly = ReadOnly;
122      dataGridView.ReadOnly = ReadOnly;
[3350]123    }
[2669]124
[2713]125    private void UpdateData() {
126      rowsTextBox.Text = Content.Rows.ToString();
[2973]127      rowsTextBox.Enabled = true;
[2713]128      columnsTextBox.Text = Content.Columns.ToString();
[2973]129      columnsTextBox.Enabled = true;
[8139]130      virtualRowIndices = Enumerable.Range(0, Content.Rows).ToArray();
[4707]131
[9306]132      if (Content.Columns == 0) {
133        Content.Columns = 1;
134      } else {
[4850]135        DataGridViewColumn[] columns = new DataGridViewColumn[Content.Columns];
136        for (int i = 0; i < columns.Length; ++i) {
137          var column = new DataGridViewTextBoxColumn();
138          column.FillWeight = 1;
139          columns[i] = column;
140        }
141        dataGridView.Columns.Clear();
142        dataGridView.Columns.AddRange(columns);
143      }
144
[9306]145      dataGridView.RowCount = Content.Rows;
[4798]146
[4523]147      ClearSorting();
[4199]148      UpdateColumnHeaders();
[3328]149      UpdateRowHeaders();
[4707]150
[4199]151      dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
152      dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
[2669]153      dataGridView.Enabled = true;
154    }
155
[4819]156    protected virtual void UpdateColumnHeaders() {
[4798]157      HashSet<string> invisibleColumnNames = new HashSet<string>(dataGridView.Columns.OfType<DataGridViewColumn>()
158      .Where(c => !c.Visible && !string.IsNullOrEmpty(c.HeaderText)).Select(c => c.HeaderText));
[4707]159
[4178]160      for (int i = 0; i < dataGridView.ColumnCount; i++) {
[4707]161        if (i < Content.ColumnNames.Count())
[3312]162          dataGridView.Columns[i].HeaderText = Content.ColumnNames.ElementAt(i);
163        else
[3346]164          dataGridView.Columns[i].HeaderText = "Column " + (i + 1);
[4798]165        dataGridView.Columns[i].Visible = !invisibleColumnNames.Contains(dataGridView.Columns[i].HeaderText);
[3312]166      }
167    }
[8833]168    protected virtual void UpdateRowHeaders() {
[4199]169      int index = dataGridView.FirstDisplayedScrollingRowIndex;
170      if (index == -1) index = 0;
171      int updatedRows = 0;
172      int count = dataGridView.DisplayedRowCount(true);
[3312]173
[4199]174      while (updatedRows < count) {
[8139]175        if (virtualRowIndices[index] < Content.RowNames.Count())
176          dataGridView.Rows[index].HeaderCell.Value = Content.RowNames.ElementAt(virtualRowIndices[index]);
[3314]177        else
[4199]178          dataGridView.Rows[index].HeaderCell.Value = "Row " + (index + 1);
179        if (dataGridView.Rows[index].Visible)
180          updatedRows++;
181        index++;
[3312]182      }
183    }
184
[3320]185    private void Content_RowNamesChanged(object sender, EventArgs e) {
[3328]186      if (InvokeRequired)
187        Invoke(new EventHandler(Content_RowNamesChanged), sender, e);
188      else
189        UpdateRowHeaders();
[3320]190    }
191    private void Content_ColumnNamesChanged(object sender, EventArgs e) {
[3328]192      if (InvokeRequired)
193        Invoke(new EventHandler(Content_ColumnNamesChanged), sender, e);
194      else
195        UpdateColumnHeaders();
[3320]196    }
[9306]197    private void Content_ItemChanged(object sender, MatrixValuesChangedEventArgs e) {
[2669]198      if (InvokeRequired)
[9306]199        Invoke(new EventHandler<MatrixValuesChangedEventArgs>(Content_ItemChanged), sender, e);
[9286]200      else {
201        foreach (var pos in e.Value) {
202          dataGridView.InvalidateCell(pos.Column, pos.Row);
203        }
204      }
[2669]205    }
[2713]206    private void Content_Reset(object sender, EventArgs e) {
[2669]207      if (InvokeRequired)
[2713]208        Invoke(new EventHandler(Content_Reset), sender, e);
[2669]209      else
[2713]210        UpdateData();
[2669]211    }
212
[2677]213    #region TextBox Events
214    private void rowsTextBox_Validating(object sender, CancelEventArgs e) {
[3714]215      if (ReadOnly || Locked)
216        return;
[2669]217      int i = 0;
[3335]218      if (!int.TryParse(rowsTextBox.Text, out i) || (i <= 0)) {
[2676]219        e.Cancel = true;
[3335]220        errorProvider.SetError(rowsTextBox, "Invalid Number of Rows (Valid values are positive integers larger than 0)");
[2677]221        rowsTextBox.SelectAll();
[2669]222      }
223    }
[2677]224    private void rowsTextBox_Validated(object sender, EventArgs e) {
[3430]225      if (!Content.ReadOnly) Content.Rows = int.Parse(rowsTextBox.Text);
[2677]226      errorProvider.SetError(rowsTextBox, string.Empty);
[2669]227    }
[2677]228    private void rowsTextBox_KeyDown(object sender, KeyEventArgs e) {
[2669]229      if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
[2677]230        rowsLabel.Focus();  // set focus on label to validate data
[2676]231      if (e.KeyCode == Keys.Escape) {
[2973]232        rowsTextBox.Text = Content.Rows.ToString();
[2677]233        rowsLabel.Focus();  // set focus on label to validate data
[2676]234      }
[2669]235    }
[2677]236    private void columnsTextBox_Validating(object sender, CancelEventArgs e) {
[3714]237      if (ReadOnly || Locked)
238        return;
[2677]239      int i = 0;
[3335]240      if (!int.TryParse(columnsTextBox.Text, out i) || (i <= 0)) {
[2677]241        e.Cancel = true;
[3335]242        errorProvider.SetError(columnsTextBox, "Invalid Number of Columns (Valid values are positive integers larger than 0)");
[2677]243        columnsTextBox.SelectAll();
244      }
245    }
246    private void columnsTextBox_Validated(object sender, EventArgs e) {
[3430]247      if (!Content.ReadOnly) Content.Columns = int.Parse(columnsTextBox.Text);
[2677]248      errorProvider.SetError(columnsTextBox, string.Empty);
249    }
250    private void columnsTextBox_KeyDown(object sender, KeyEventArgs e) {
251      if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
252        columnsLabel.Focus();  // set focus on label to validate data
253      if (e.KeyCode == Keys.Escape) {
[2713]254        columnsTextBox.Text = Content.Columns.ToString();
[2677]255        columnsLabel.Focus();  // set focus on label to validate data
256      }
257    }
258    #endregion
259
260    #region DataGridView Events
[2669]261    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
[9307]262      // RowIndex and ColumnIndex have to be checked, if they are in bounds, otherwise Cancel might be set, which can lead to exceptions
263      if (!dataGridView.ReadOnly && e.RowIndex < Content.Rows && e.ColumnIndex < Content.Columns) {
[3328]264        string errorMessage;
[4030]265        if (Content != null && !Content.Validate(e.FormattedValue.ToString(), out errorMessage)) {
[9307]266          e.Cancel = true;
[3328]267          dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
268        }
[2676]269      }
[2669]270    }
[2676]271    private void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
[3328]272      if (!dataGridView.ReadOnly) {
273        string value = e.Value.ToString();
[8139]274        int rowIndex = virtualRowIndices[e.RowIndex];
[3328]275        e.ParsingApplied = Content.SetValue(value, rowIndex, e.ColumnIndex);
276        if (e.ParsingApplied) e.Value = Content.GetValue(rowIndex, e.ColumnIndex);
277      }
[2669]278    }
[2676]279    private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
280      dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
281    }
[3312]282    private void dataGridView_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e) {
[4011]283      if (Content != null && e.RowIndex < Content.Rows && e.ColumnIndex < Content.Columns) {
[8139]284        int rowIndex = virtualRowIndices[e.RowIndex];
[3335]285        e.Value = Content.GetValue(rowIndex, e.ColumnIndex);
[3337]286      }
[3312]287    }
[4178]288
289    private void dataGridView_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e) {
290      this.UpdateRowHeaders();
[3312]291    }
292    private void dataGridView_Resize(object sender, EventArgs e) {
[4178]293      this.UpdateRowHeaders();
[3312]294    }
[3314]295
[3643]296    private void dataGridView_KeyDown(object sender, KeyEventArgs e) {
[4178]297      if (!ReadOnly && e.Control && e.KeyCode == Keys.V)
298        PasteValuesToDataGridView();
299      else if (e.Control && e.KeyCode == Keys.C)
300        CopyValuesFromDataGridView();
301    }
[3643]302
[4178]303    private void CopyValuesFromDataGridView() {
304      if (dataGridView.SelectedCells.Count == 0) return;
305      StringBuilder s = new StringBuilder();
306      int minRowIndex = dataGridView.SelectedCells[0].RowIndex;
307      int maxRowIndex = dataGridView.SelectedCells[dataGridView.SelectedCells.Count - 1].RowIndex;
308      int minColIndex = dataGridView.SelectedCells[0].ColumnIndex;
309      int maxColIndex = dataGridView.SelectedCells[dataGridView.SelectedCells.Count - 1].ColumnIndex;
310
311      if (minRowIndex > maxRowIndex) {
312        int temp = minRowIndex;
313        minRowIndex = maxRowIndex;
314        maxRowIndex = temp;
315      }
316      if (minColIndex > maxColIndex) {
317        int temp = minColIndex;
318        minColIndex = maxColIndex;
319        maxColIndex = temp;
320      }
321
[4546]322      bool addRowNames = dataGridView.AreAllCellsSelected(false) && Content.RowNames.Count() > 0;
323      bool addColumnNames = dataGridView.AreAllCellsSelected(false) && Content.ColumnNames.Count() > 0;
[4178]324
325      //add colum names
326      if (addColumnNames) {
327        if (addRowNames)
328          s.Append('\t');
329
330        DataGridViewColumn column = dataGridView.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
331        while (column != null) {
332          s.Append(column.HeaderText);
333          s.Append('\t');
334          column = dataGridView.Columns.GetNextColumn(column, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
[3643]335        }
[4178]336        s.Remove(s.Length - 1, 1); //remove last tab
337        s.Append(Environment.NewLine);
338      }
[3643]339
[4178]340      for (int i = minRowIndex; i <= maxRowIndex; i++) {
[8139]341        int rowIndex = this.virtualRowIndices[i];
[4178]342        if (addRowNames) {
343          s.Append(Content.RowNames.ElementAt(rowIndex));
344          s.Append('\t');
345        }
346
347        DataGridViewColumn column = dataGridView.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
348        while (column != null) {
349          DataGridViewCell cell = dataGridView[column.Index, i];
350          if (cell.Selected) {
351            s.Append(Content.GetValue(rowIndex, column.Index));
[4600]352            s.Append('\t');
[3643]353          }
[4600]354
[4178]355          column = dataGridView.Columns.GetNextColumn(column, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
[3643]356        }
[4178]357        s.Remove(s.Length - 1, 1); //remove last tab
358        s.Append(Environment.NewLine);
359      }
360      Clipboard.SetText(s.ToString());
361    }
[3643]362
[4178]363    private void PasteValuesToDataGridView() {
364      string[,] values = SplitClipboardString(Clipboard.GetText());
365      int rowIndex = 0;
366      int columnIndex = 0;
367      if (dataGridView.CurrentCell != null) {
368        rowIndex = dataGridView.CurrentCell.RowIndex;
369        columnIndex = dataGridView.CurrentCell.ColumnIndex;
[3643]370      }
[7984]371      if (Content.Rows < values.GetLength(1) + rowIndex) Content.Rows = values.GetLength(1) + rowIndex;
372      if (Content.Columns < values.GetLength(0) + columnIndex) Content.Columns = values.GetLength(0) + columnIndex;
[4178]373
[9306]374      List<MatrixValue<string>> setValues = new List<MatrixValue<string>>(values.GetLength(0) * values.GetLength(1));
[4178]375      for (int row = 0; row < values.GetLength(1); row++) {
376        for (int col = 0; col < values.GetLength(0); col++) {
[9306]377          setValues.Add(new MatrixValue<string>(new MatrixPosition(row + rowIndex, col + columnIndex), values[col, row]));
[4178]378        }
379      }
[9306]380      Content.SetValues(setValues);
[4178]381      ClearSorting();
[3643]382    }
383    private string[,] SplitClipboardString(string clipboardText) {
[7987]384      if (clipboardText.EndsWith(Environment.NewLine))
385        clipboardText = clipboardText.Remove(clipboardText.Length - Environment.NewLine.Length);  //remove last newline constant
[3643]386      string[,] values = null;
387      string[] lines = clipboardText.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
388      string[] cells;
389      for (int i = 0; i < lines.Length; i++) {
390        cells = lines[i].Split('\t');
391        if (values == null)
392          values = new string[cells.Length, lines.Length];
393        for (int j = 0; j < cells.Length; j++)
394          values[j, i] = string.IsNullOrEmpty(cells[j]) ? string.Empty : cells[j];
395      }
396      return values;
397    }
398
[3314]399    private void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
[3320]400      if (Content != null) {
401        if (e.Button == MouseButtons.Left && Content.SortableView) {
[8139]402          bool addToSortedIndices = (Control.ModifierKeys & Keys.Control) == Keys.Control;
[3320]403          SortOrder newSortOrder = SortOrder.Ascending;
[8139]404          if (sortedColumnIndices.Any(x => x.Key == e.ColumnIndex)) {
405            SortOrder oldSortOrder = sortedColumnIndices.Where(x => x.Key == e.ColumnIndex).First().Value;
[3320]406            int enumLength = Enum.GetValues(typeof(SortOrder)).Length;
407            newSortOrder = oldSortOrder = (SortOrder)Enum.Parse(typeof(SortOrder), ((((int)oldSortOrder) + 1) % enumLength).ToString());
408          }
[3314]409
[8139]410          if (!addToSortedIndices)
411            sortedColumnIndices.Clear();
[3314]412
[8139]413          if (sortedColumnIndices.Any(x => x.Key == e.ColumnIndex)) {
414            int sortedIndex = sortedColumnIndices.FindIndex(x => x.Key == e.ColumnIndex);
[3320]415            if (newSortOrder != SortOrder.None)
[8139]416              sortedColumnIndices[sortedIndex] = new KeyValuePair<int, SortOrder>(e.ColumnIndex, newSortOrder);
[3320]417            else
[8139]418              sortedColumnIndices.RemoveAt(sortedIndex);
[3320]419          } else
420            if (newSortOrder != SortOrder.None)
[8139]421              sortedColumnIndices.Add(new KeyValuePair<int, SortOrder>(e.ColumnIndex, newSortOrder));
[3320]422          Sort();
423        }
[3314]424      }
425    }
426
[4518]427    protected virtual void ClearSorting() {
[8139]428      virtualRowIndices = Enumerable.Range(0, Content.Rows).ToArray();
429      sortedColumnIndices.Clear();
[3643]430      UpdateSortGlyph();
431    }
432
[3314]433    private void Sort() {
[8139]434      virtualRowIndices = Sort(sortedColumnIndices);
[3444]435      UpdateSortGlyph();
[3546]436      UpdateRowHeaders();
[3444]437      dataGridView.Invalidate();
438    }
439    protected virtual int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
[3328]440      int[] newSortedIndex = Enumerable.Range(0, Content.Rows).ToArray();
[3444]441      if (sortedColumns.Count() != 0) {
[8139]442        rowComparer.SortedIndices = sortedColumns;
[3346]443        rowComparer.Matrix = Content;
[3314]444        Array.Sort(newSortedIndex, rowComparer);
445      }
[3444]446      return newSortedIndex;
[3328]447    }
448    private void UpdateSortGlyph() {
[3314]449      foreach (DataGridViewColumn col in this.dataGridView.Columns)
450        col.HeaderCell.SortGlyphDirection = SortOrder.None;
[8139]451      foreach (KeyValuePair<int, SortOrder> p in sortedColumnIndices)
[3314]452        this.dataGridView.Columns[p.Key].HeaderCell.SortGlyphDirection = p.Value;
453    }
[2677]454    #endregion
[3312]455
[3346]456    public class RowComparer : IComparer<int> {
[3314]457      public RowComparer() {
458      }
[3312]459
[8139]460      private List<KeyValuePair<int, SortOrder>> sortedIndices;
461      public IEnumerable<KeyValuePair<int, SortOrder>> SortedIndices {
462        get { return this.sortedIndices; }
463        set { sortedIndices = new List<KeyValuePair<int, SortOrder>>(value); }
[3346]464      }
465      private IStringConvertibleMatrix matrix;
466      public IStringConvertibleMatrix Matrix {
467        get { return this.matrix; }
468        set { this.matrix = value; }
469      }
470
[3314]471      public int Compare(int x, int y) {
472        int result = 0;
473        double double1, double2;
474        DateTime dateTime1, dateTime2;
[3444]475        TimeSpan timeSpan1, timeSpan2;
[3314]476        string string1, string2;
477
[3346]478        if (matrix == null)
479          throw new InvalidOperationException("Could not sort IStringConvertibleMatrix if the matrix member is null.");
[8139]480        if (sortedIndices == null)
[3346]481          return 0;
482
[8139]483        foreach (KeyValuePair<int, SortOrder> pair in sortedIndices.Where(p => p.Value != SortOrder.None)) {
[3350]484          string1 = matrix.GetValue(x, pair.Key);
485          string2 = matrix.GetValue(y, pair.Key);
486          if (double.TryParse(string1, out double1) && double.TryParse(string2, out double2))
487            result = double1.CompareTo(double2);
488          else if (DateTime.TryParse(string1, out dateTime1) && DateTime.TryParse(string2, out dateTime2))
489            result = dateTime1.CompareTo(dateTime2);
[3444]490          else if (TimeSpan.TryParse(string1, out timeSpan1) && TimeSpan.TryParse(string2, out timeSpan2))
491            result = timeSpan1.CompareTo(timeSpan2);
[3350]492          else {
493            if (string1 != null)
494              result = string1.CompareTo(string2);
495            else if (string2 != null)
496              result = string2.CompareTo(string1) * -1;
497          }
498          if (pair.Value == SortOrder.Descending)
499            result *= -1;
500          if (result != 0)
501            return result;
[3314]502        }
503        return result;
504      }
505    }
[3316]506
[4213]507    private void dataGridView_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e) {
508      if (Content == null) return;
509      if (e.Button == MouseButtons.Right && Content.ColumnNames.Count() != 0)
510        contextMenu.Show(MousePosition);
511    }
[8833]512    protected virtual void ShowHideColumns_Click(object sender, EventArgs e) {
513      new StringConvertibleMatrixColumnVisibilityDialog(this.dataGridView.Columns.Cast<DataGridViewColumn>()).ShowDialog();
[3316]514    }
[4652]515
516    private void UpdateVisibilityOfTextBoxes() {
517      rowsTextBox.Visible = columnsTextBox.Visible = showRowsAndColumnsTextBox;
518      rowsLabel.Visible = columnsLabel.Visible = showRowsAndColumnsTextBox;
[4709]519      UpdateDataGridViewSizeAndLocation();
520    }
[4652]521
[4709]522    private void UpdateVisibilityOfStatisticalInformation() {
[4779]523      statisticsTextBox.Visible = showStatisticalInformation;
[4709]524      UpdateDataGridViewSizeAndLocation();
525    }
526
527    private void UpdateDataGridViewSizeAndLocation() {
[4652]528      int headerSize = columnsTextBox.Location.Y + columnsTextBox.Size.Height +
[4709]529       columnsTextBox.Margin.Bottom + dataGridView.Margin.Top;
[4652]530
531      int offset = showRowsAndColumnsTextBox ? headerSize : 0;
532      dataGridView.Location = new Point(0, offset);
[4709]533
[4779]534      int statisticsTextBoxHeight = showStatisticalInformation ? statisticsTextBox.Height + statisticsTextBox.Margin.Top + statisticsTextBox.Margin.Bottom : 0;
535      dataGridView.Size = new Size(Size.Width, Size.Height - offset - statisticsTextBoxHeight);
[4652]536    }
[4709]537
538    private void dataGridView_SelectionChanged(object sender, EventArgs e) {
[4779]539      string stringFormat = "{0,20:0.0000}";
540      statisticsTextBox.Text = string.Empty;
[4709]541      if (dataGridView.SelectedCells.Count > 1) {
542        List<double> selectedValues = new List<double>();
543        foreach (DataGridViewCell cell in dataGridView.SelectedCells) {
544          double value;
[9286]545          if (cell.Value == null || !double.TryParse(cell.Value.ToString(), out value)) return;
[4709]546          selectedValues.Add(value);
547        }
548        if (selectedValues.Count > 1) {
549          StringBuilder labelText = new StringBuilder();
[4779]550          labelText.Append("Count: " + string.Format(stringFormat, selectedValues.Count) + "    ");
551          labelText.Append("Sum: " + string.Format(stringFormat, selectedValues.Sum()) + "    ");
552          labelText.Append("Min: " + string.Format(stringFormat, selectedValues.Min()) + "    ");
553          labelText.Append("Max: " + string.Format(stringFormat, selectedValues.Max()) + "    ");
554          labelText.Append("Average: " + string.Format(stringFormat, selectedValues.Average()) + "    ");
555          labelText.Append("Standard Deviation: " + string.Format(stringFormat, selectedValues.StandardDeviation()) + "    ");
[4720]556
[4779]557          statisticsTextBox.Text = labelText.ToString();
[4709]558        }
559      }
560    }
[2669]561  }
562}
Note: See TracBrowser for help on using the repository browser.