Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataAnalysis/DatasetView.cs @ 202

Last change on this file since 202 was 168, checked in by gkronber, 17 years ago

fixed #118

File size: 3.2 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
45    public DatasetView(Dataset dataset)
46      : this() {
47      this.Dataset = dataset;
48    }
49
50    protected override void UpdateControls() {
51      base.UpdateControls();
52      if (Dataset != null) {
53        int rows = Dataset.Rows;
54        int columns = Dataset.Columns;
55        nameTextBox.Text = Dataset.Name;
56        rowsTextBox.Text = rows + "";
57        columnsTextBox.Text = columns + "";
58        dataGridView.ColumnCount = columns;
59        dataGridView.RowCount = rows;
60        for (int i = 0; i < rows; i++) {
61          for (int j = 0; j < columns; j++) {
62            dataGridView.Rows[i].Cells[j].Value = Dataset.GetValue(i, j);
63          }
64        }
65        if (Dataset.VariableNames.Length == dataGridView.Columns.Count) {
66          for (int i = 0; i < columns; i++) {
67            dataGridView.Columns[i].Name = Dataset.VariableNames[i];
68          }
69        } else {
70          for (int i = 0; i < columns; i++) {
71            dataGridView.Columns[i].Name = "Var" + i;
72          }
73        }
74
75      } else {
76        rowsTextBox.Text = "1";
77        columnsTextBox.Text = "1";
78        dataGridView.ColumnCount = 1;
79        dataGridView.RowCount = 1;
80      }
81    }
82
83    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
84      if (ValidateData((string)e.FormattedValue)) {
85        SetArrayElement(e.RowIndex, e.ColumnIndex, (string)e.FormattedValue);
86        e.Cancel = false;
87      } else {
88        e.Cancel = true;
89      }
90    }
91
92
93    private void SetArrayElement(int row, int column, string element) {
94      double result;
95      double.TryParse(element, out result);
96      if (result != Dataset.GetValue(row, column)) {
97        Dataset.SetValue(row, column, result);
98        Dataset.FireChanged();
99      }
100    }
101
102    private bool ValidateData(string element) {
103      double result;
104      return element != null && double.TryParse(element, out result);
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.