Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.0/sources/HeuristicLab.DataAnalysis/DatasetView.cs @ 771

Last change on this file since 771 was 345, checked in by gkronber, 17 years ago
  • merged r338 r341 and r343 from the ticket-specific trunk into the HL3.0 stable branch
  • fixed serialization and display of floating point numbers in HL3.0 stable plugins HeuristicLab.Functions and HeuristicLab.DataAnalysis

(ticket #175)

File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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;
23using System.Windows.Forms;
24using HeuristicLab.Core;
25
26namespace HeuristicLab.DataAnalysis {
27  public partial class DatasetView : ViewBase {
28
29    private OpenFileDialog openFileDialog;
30
31    public Dataset Dataset {
32      get { return (Dataset)Item; }
33      set {
34        Item = value;
35        Refresh();
36      }
37    }
38
39    public DatasetView()
40      : base() {
41      InitializeComponent();
42      openFileDialog = new OpenFileDialog();
43
44      dataGridView.DefaultCellStyle.Format = "r";
45    }
46
47    public DatasetView(Dataset dataset)
48      : this() {
49      this.Dataset = dataset;
50    }
51
52    protected override void UpdateControls() {
53      base.UpdateControls();
54      if (Dataset != null) {
55        int rows = Dataset.Rows;
56        int columns = Dataset.Columns;
57        nameTextBox.Text = Dataset.Name;
58        rowsTextBox.Text = rows + "";
59        columnsTextBox.Text = columns + "";
60        dataGridView.ColumnCount = columns;
61        dataGridView.RowCount = rows;
62        for (int i = 0; i < rows; i++) {
63          for (int j = 0; j < columns; j++) {
64            dataGridView.Rows[i].Cells[j].Value = Dataset.GetValue(i, j);
65          }
66        }
67        if (Dataset.VariableNames.Length == dataGridView.Columns.Count) {
68          for (int i = 0; i < columns; i++) {
69            dataGridView.Columns[i].Name = Dataset.VariableNames[i];
70          }
71        } else {
72          for (int i = 0; i < columns; i++) {
73            dataGridView.Columns[i].Name = "Var" + i;
74          }
75        }
76
77      } else {
78        rowsTextBox.Text = "1";
79        columnsTextBox.Text = "1";
80        dataGridView.ColumnCount = 1;
81        dataGridView.RowCount = 1;
82      }
83    }
84
85    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
86      if (ValidateData((string)e.FormattedValue)) {
87        SetArrayElement(e.RowIndex, e.ColumnIndex, (string)e.FormattedValue);
88        e.Cancel = false;
89      } else {
90        e.Cancel = true;
91      }
92    }
93
94
95    private void SetArrayElement(int row, int column, string element) {
96      double result;
97      double.TryParse(element, out result);
98      if (result != Dataset.GetValue(row, column)) {
99        Dataset.SetValue(row, column, result);
100        Dataset.FireChanged();
101      }
102    }
103
104    private bool ValidateData(string element) {
105      double result;
106      return element != null && double.TryParse(element, out result);
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.