Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FunctionsAndStructIdRefactoring/HeuristicLab.DataAnalysis/DatasetView.cs @ 148

Last change on this file since 148 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

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        Item.Changed += new EventHandler(Item_Changed);
36      }
37    }
38
39    void Item_Changed(object sender, EventArgs e) {
40      Refresh();
41    }
42
43    public DatasetView() : base() {
44      InitializeComponent();
45      openFileDialog = new OpenFileDialog();
46    }
47
48    public DatasetView(Dataset dataset) :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
58        nameTextBox.Text = Dataset.Name;
59        rowsTextBox.Text = rows + "";
60        columnsTextBox.Text = columns + "";
61        dataGridView.ColumnCount = columns;
62        dataGridView.RowCount = rows;
63        for(int i = 0; i < rows; i++) {
64          for(int j = 0; j < columns; j++) {
65            dataGridView.Rows[i].Cells[j].Value = Dataset.GetValue(i, j);
66          }
67        }
68        if (Dataset.VariableNames.Length == dataGridView.Columns.Count) {
69          for (int i = 0; i < columns; i++) {
70            dataGridView.Columns[i].Name = Dataset.VariableNames[i];
71          }
72        } else {
73          for (int i = 0; i < columns; i++) {
74            dataGridView.Columns[i].Name = "Var" + i;
75          }
76        }
77
78      } else {
79        rowsTextBox.Text = "1";
80        columnsTextBox.Text = "1";
81        dataGridView.ColumnCount = 1;
82        dataGridView.RowCount = 1;
83      }
84    }
85
86    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
87      if(ValidateData((string)e.FormattedValue)) {
88        SetArrayElement(e.RowIndex, e.ColumnIndex, (string)e.FormattedValue);
89        e.Cancel = false;
90      } else {
91        e.Cancel = true;
92      }
93    }
94
95
96    private void SetArrayElement(int row, int column, string element) {
97      double result;
98      double.TryParse(element, out result);
99      if(result != Dataset.GetValue(row, column)) {
100        Dataset.SetValue(row, column, result);
101        Dataset.FireChanged();
102      }
103    }
104
105    private bool ValidateData(string element) {
106      double result;
107      return element != null && double.TryParse(element, out result);
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.