Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Data/View/ColumnGroupView.cs @ 12467

Last change on this file since 12467 was 9640, checked in by mkommend, 12 years ago

#1734: Updated save file dialog and ColumnGroupView in the data importer.

File size: 26.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using System.Text;
27using System.Windows.Forms;
28using HeuristicLab.DataImporter.Data.Command;
29using HeuristicLab.DataImporter.Data.Model;
30
31
32namespace HeuristicLab.DataImporter.Data.View {
33  public delegate void ColumnGroupActivatedEventHandler(object sender, bool addToActiveColumnGroups);
34  public partial class ColumnGroupView : UserControl {
35
36    private TextBox txtColumnName;
37    private ColumnGroupView()
38      : base() {
39      InitializeComponent();
40      txtColumnName = new TextBox();
41      this.splitContainer1.Panel2.Controls.Add(txtColumnName);
42      txtColumnName.Visible = false;
43      txtColumnName.Validated += new EventHandler(txtColumnName_Validated);
44      txtColumnName.KeyDown += new KeyEventHandler(txtColumnName_KeyDown);
45
46      this.dataGridView.EnableHeadersVisualStyles = false;
47      this.dataGridView.Dock = DockStyle.Top | DockStyle.Bottom;
48      this.dataGridView.VirtualMode = true;
49      this.dataGridView.ShowCellToolTips = false;
50
51      this.dataGridView.ColumnHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView_ColumnHeaderMouseClick);
52      this.dataGridView.ColumnHeaderMouseDoubleClick += new DataGridViewCellMouseEventHandler(dataGridView_ColumnHeaderMouseDoubleClick);
53      this.dataGridView.ColumnWidthChanged += new DataGridViewColumnEventHandler(dataGridView_ColumnWidthChanged);
54      this.dataGridView.MouseClick += new MouseEventHandler(dataGridView_MouseClick);
55      this.dataGridView.SelectionChanged += new EventHandler(dataGridView_SelectionChanged);
56      this.dataGridView.AllowUserToOrderColumnsChanged += new EventHandler(dataGridView_AllowUserToOrderColumnsChanged);
57      this.dataGridView.KeyDown += new KeyEventHandler(dataGridView_KeyDown);
58      this.dataGridView.RowHeadersWidthChanged += new EventHandler(dataGridView_RowHeadersWidthChanged);
59      this.dataGridView.CellMouseEnter += new DataGridViewCellEventHandler(dataGridView_CellMouseEnter);
60      this.dataGridView.Scroll += new ScrollEventHandler(dataGridView_Scroll);
61      this.dataGridView.Resize += new EventHandler(dataGridView_Resize);
62
63      //delegates for virtual mode
64      this.dataGridView.CellValueNeeded += new DataGridViewCellValueEventHandler(dataGridView_CellValueNeeded);
65      this.dataGridView.CellValuePushed += new DataGridViewCellValueEventHandler(dataGridView_CellValuePushed);
66      this.dataGridView.UserDeletingRow += new DataGridViewRowCancelEventHandler(dataGridView_UserDeletingRow);
67    }
68
69    private ColumnGroupView(ColumnGroup columnGroup)
70      : this() {
71      this.ColumnGroup = columnGroup;
72      this.ColumnGroup.Changed += this.ColumnGroupChanged;
73      this.dataGridView.ClearSelection();
74      this.state = ColumnGroupState.None;
75      this.UpdateDataGridView();
76    }
77
78    public ColumnGroupView(ColumnGroup columnGroup, CommandChain commandChain)
79      : this(columnGroup) {
80      this.commandChain = commandChain;
81    }
82
83    public bool ColumnGroupActive {
84      get { return this.ColumnGroup.Active; }
85      set {
86        this.ColumnGroup.Active = value;
87        if (this.ColumnGroup.Active)
88          this.txtColumnGroupName.BackColor = Color.LightGreen;
89        else {
90          this.dataGridView.ClearSelection();
91          this.txtColumnGroupName.BackColor = Control.DefaultBackColor;
92        }
93        UpdateStateInformation();
94      }
95    }
96
97    private ColumnGroup columnGroup;
98    public ColumnGroup ColumnGroup {
99      get { return (ColumnGroup)this.columnGroup; }
100      private set { this.columnGroup = value; }
101    }
102
103    private Data.Model.DataSet DataSet {
104      get { return ((DataSetView)this.Parent).DataSet; }
105    }
106
107    private CommandChain commandChain;
108    public CommandChain CommandChain {
109      get { return this.commandChain; }
110      set { this.commandChain = value; }
111    }
112
113    public bool AllowReorderColumns {
114      get { return this.dataGridView.AllowUserToOrderColumns; }
115      set { this.dataGridView.AllowUserToOrderColumns = value; }
116    }
117
118    public int[] DisplayIndexes {
119      get {
120        int[] ret = new int[this.ColumnGroup.Columns.Count()];
121        for (int i = 0; i < this.dataGridView.Columns.Count; i++) {
122          ret[dataGridView.Columns[i].DisplayIndex] = i;
123        }
124        return ret;
125      }
126    }
127
128    public int MaxWidth {
129      get { return this.MaximumSize.Width; }
130      set {
131        this.MaximumSize = new Size(value, this.MaximumSize.Height);
132        this.dataGridView.MaximumSize = new Size(value, this.MaximumSize.Height);
133        RecalculateWidthOfControl();
134      }
135    }
136
137    //IMPORTANT: use the defined property to change the state, because the event StateChanged must be fired!
138    private ColumnGroupState state;
139    public ColumnGroupState State {
140      get { return this.state; }
141      protected set {
142        this.state = value;
143        FireStateChanged();
144      }
145    }
146
147    public event EventHandler StateChanged;
148    protected void FireStateChanged() {
149      OnStateChanged();
150    }
151
152    protected virtual void OnStateChanged() {
153      if (StateChanged != null) {
154        StateChanged(this, new EventArgs());
155      }
156    }
157
158    public event ColumnGroupActivatedEventHandler Activated;
159    public void FireActivated(bool addToActiveColumnGroups) {
160      OnActivated(addToActiveColumnGroups);
161    }
162
163    protected virtual void OnActivated(bool addToActiveColumnGroups) {
164      if (Activated != null) {
165        Activated(this, addToActiveColumnGroups);
166      }
167    }
168
169    private void UpdateStateInformation() {
170      this.UpdateStateInformation(false);
171    }
172
173    private void UpdateStateInformation(bool selectionChanged) {
174      ColumnGroupState newState = this.ColumnGroupActive ? ColumnGroupState.Active : ColumnGroupState.None;
175      foreach (ColumnBase col in ColumnGroup.SelectedColumns) {
176        if (col is DoubleColumn)
177          newState |= ColumnGroupState.DoubleColumnSelected;
178        else if (col is StringColumn)
179          newState |= ColumnGroupState.StringColumnSelected;
180        else if (col is DateTimeColumn)
181          newState |= ColumnGroupState.DateTimeColumnSelected;
182        else if (col is ProgrammableColumn)
183          newState |= ColumnGroupState.ProgrammableColumnSelected;
184        if (col.ContainsNullValues)
185          newState |= ColumnGroupState.AnySelectedColumnContainsNull;
186      }
187      if (ColumnGroup.Sorted) {
188        if (ColumnGroup.SelectedColumns.Any(col => col.SortOrder == SortOrder.None))
189          newState |= ColumnGroupState.AnySelectedColumnNotSorted;
190        else
191          newState |= ColumnGroupState.Sorted;
192      }
193      if (newState != this.State || selectionChanged)
194        this.State = newState;
195    }
196
197    private void PasteClipboardContent() {
198      if (dataGridView.CurrentCell != null) {
199        string values = Clipboard.GetText();
200        values = values.Remove(values.Length - Environment.NewLine.Length);
201        this.commandChain.Add(new PasteValuesCommand(DataSet, this.ColumnGroup.Name, dataGridView.CurrentCell.ColumnIndex,
202          dataGridView.CurrentCell.RowIndex, values));
203      }
204    }
205
206    private void CopyClipboardContent() {
207      if (dataGridView.SelectedCells.Count != 0) {
208        StringBuilder s = new StringBuilder();
209        DataGridViewCell cell;
210        int minRowIndex = dataGridView.SelectedCells[0].RowIndex;
211        int maxRowIndex = dataGridView.SelectedCells[dataGridView.SelectedCells.Count - 1].RowIndex;
212        int minColIndex = dataGridView.SelectedCells[0].ColumnIndex;
213        int maxColIndex = dataGridView.SelectedCells[dataGridView.SelectedCells.Count - 1].ColumnIndex;
214
215        if (minRowIndex > maxRowIndex) {
216          int temp = minRowIndex;
217          minRowIndex = maxRowIndex;
218          maxRowIndex = temp;
219        }
220
221        if (minColIndex > maxColIndex) {
222          int temp = minColIndex;
223          minColIndex = maxColIndex;
224          maxColIndex = temp;
225        }
226        if (maxRowIndex == dataGridView.RowCount - 1)
227          maxRowIndex--;
228
229        for (int i = minRowIndex; i < maxRowIndex + 1; i++) {
230          for (int j = minColIndex; j < maxColIndex + 1; j++) {
231            cell = dataGridView[j, i];
232            if (cell.Selected) {
233              if (cell.Value != null)
234                s.Append(cell.Value.ToString());
235            }
236            if (j != maxColIndex)
237              s.Append("\t");
238          }
239          s.Append(Environment.NewLine);
240        }
241        Clipboard.SetText(s.ToString());
242      }
243    }
244
245    public override void Refresh() {
246      base.Refresh();
247      if (ColumnGroup != null) {
248        this.UpdateDataGridView();
249      } else {
250        dataGridView.ColumnCount = 0;
251        dataGridView.RowCount = 0;
252      }
253    }
254
255    public void ColumnGroupChanged(object sender, EventArgs e) {
256      this.UpdateDataGridView();
257    }
258
259    private void UpdateDataGridView() {
260      int firstVisibleRowIndex = 0;
261      int firstVisibleColIndex = 0;
262      if (dataGridView.FirstDisplayedCell != null) {
263        if (firstVisibleColIndex < ColumnGroup.Columns.Count())
264          firstVisibleColIndex = dataGridView.FirstDisplayedCell.ColumnIndex;
265        if (firstVisibleRowIndex < ColumnGroup.RowCount)
266          firstVisibleRowIndex = dataGridView.FirstDisplayedCell.RowIndex;
267      }
268
269      //needed because otherwise columns could not be added
270      this.dataGridView.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
271      if (dataGridView.ColumnCount != ColumnGroup.Columns.Count()) {
272        this.dataGridView.ColumnCount = ColumnGroup.Columns.Count();
273        for (int i = 0; i < ColumnGroup.Columns.Count(); i++) {
274          dataGridView.Columns[i].SortMode = DataGridViewColumnSortMode.Programmatic;
275        }
276      }
277      for (int i = 0; i < ColumnGroup.Columns.Count(); i++)
278        dataGridView.Columns[i].HeaderText = ColumnGroup.Columns.ElementAt(i).ToString().Insert(ColumnGroup.Columns.ElementAt(i).ToString().IndexOf('<'), "\n");
279
280      // needed for performance reasons; further information at http://whiletrue.nl/blog/?p=38
281      if (dataGridView.RowCount != ColumnGroup.RowCount + (ColumnGroup.Columns.Count() == 0 ? 0 : 1)) {
282        //event handler must be deregistered cause otherwise cellvaluepushed is fired again
283        this.dataGridView.CellValuePushed -= new DataGridViewCellValueEventHandler(dataGridView_CellValuePushed);
284        if (Math.Abs(dataGridView.RowCount - ColumnGroup.RowCount) > 10)
285          dataGridView.Rows.Clear();
286        bool rowAdded = dataGridView.RowCount == ColumnGroup.RowCount && dataGridView.RowCount != 0;
287        dataGridView.RowCount = ColumnGroup.RowCount + (!ColumnGroup.Columns.Any() ? 0 : 1);
288
289        if (rowAdded) {
290          Point p = this.dataGridView.CurrentCellAddress;
291          p.Y += 1;
292          this.dataGridView.CurrentCell = this.dataGridView.Rows[p.Y].Cells[p.X];
293          this.dataGridView.ClearSelection();
294          this.dataGridView.CurrentCell.Selected = true;
295        }
296        this.dataGridView.CellValuePushed += new DataGridViewCellValueEventHandler(dataGridView_CellValuePushed);
297      }
298
299      UpdateDataGridViewHeaderCells();
300      this.dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
301      this.dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
302      if (dataGridView.RowCount != 0 && dataGridView.ColumnCount != 0) {
303        if (firstVisibleColIndex >= dataGridView.ColumnCount)
304          firstVisibleColIndex = 0;
305        if (firstVisibleRowIndex >= dataGridView.RowCount)
306          firstVisibleRowIndex = 0;
307        dataGridView.FirstDisplayedCell = dataGridView[firstVisibleColIndex, firstVisibleRowIndex];
308      }
309
310      UpdateSortGlyph();
311      this.txtColumnGroupName.Text = this.ColumnGroup.Name + "  " + ColumnGroup.RowCount + " rows";
312      RecalculateWidthOfControl();
313      this.dataGridView.Invalidate();
314      UpdateStateInformation();
315    }
316
317    private void UpdateDataGridViewHeaderCells() {
318      int index = dataGridView.FirstDisplayedScrollingRowIndex;
319      if (index == -1) index = 0;
320      int updatedRows = 0;
321      int count = dataGridView.DisplayedRowCount(true);
322
323      while (updatedRows < count) {
324        dataGridView.Rows[index].HeaderCell.Value = (index + 1).ToString();
325        if (dataGridView.Rows[index].Visible)
326          updatedRows++;
327        index++;
328      }
329    }
330
331    private void dataGridView_ColumnHeaderMouseClick(object sender, System.Windows.Forms.DataGridViewCellMouseEventArgs e) {
332      if (e.Button == MouseButtons.Right) {
333        this.commandChain.Add(new SortCommand(DataSet, this.ColumnGroup.Name,
334          e.ColumnIndex, (Control.ModifierKeys & Keys.Control) == Keys.Control));
335      }
336    }
337
338    private void dataGridView_ColumnHeaderMouseDoubleClick(object sender, System.Windows.Forms.DataGridViewCellMouseEventArgs e) {
339      if (e.Button == MouseButtons.Left) {
340        dataGridView.ClearSelection();
341        Rectangle rect = dataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
342        ColumnBase col = this.ColumnGroup.Columns.ElementAt(e.ColumnIndex);
343        txtColumnName.Text = col.Name;
344        txtColumnName.Size = rect.Size;
345        txtColumnName.Location = rect.Location;
346        txtColumnName.Visible = true;
347        txtColumnName.Focus();
348        txtColumnName.BringToFront();
349      }
350    }
351
352    private void UpdateSortGlyph() {
353      System.Collections.IEnumerator e = this.dataGridView.Columns.GetEnumerator();
354      e.MoveNext();
355      foreach (SortOrder sortOrder in this.ColumnGroup.SortOrdersForColumns) {
356        ((DataGridViewColumn)e.Current).HeaderCell.SortGlyphDirection = sortOrder;
357        e.MoveNext();
358      }
359    }
360
361    private void dataGridView_SelectionChanged(object sender, EventArgs e) {
362      int nullValuesCount = 0;
363      int totalValuesCount = 0;
364      IComparable minimum = "";
365      IComparable maximum = "";
366      double? mean = null;
367      double? median = null;
368      double? stddev = null;
369      ColumnBase column;
370
371      this.ColumnGroup.ClearSelectedColumns();
372      if (this.dataGridView.SelectedColumns.Count != 0) {
373        foreach (DataGridViewColumn col in this.dataGridView.SelectedColumns) {
374          column = this.ColumnGroup.Columns.ElementAt(col.Index);
375          column.Selected = true;
376          nullValuesCount += column.NullValuesCount;
377          totalValuesCount += column.NonNullValuesCount;
378        }
379        if (this.dataGridView.SelectedColumns.Count == 1) {
380          column = this.ColumnGroup.Columns.ElementAt(dataGridView.SelectedColumns[0].Index);
381          minimum = column.Minimum;
382          maximum = column.Maximum;
383          if (column is DoubleColumn) {
384            mean = ((DoubleColumn)column).Mean;
385            stddev = ((DoubleColumn)column).StandardDeviation;
386            median = ((DoubleColumn)column).Median;
387          }
388        }
389      }
390      lblNullValues.Text = nullValuesCount.ToString();
391      double nullPercentage = (100.0 / (double)(totalValuesCount + nullValuesCount)) * (double)nullValuesCount;
392      lblNullPercentage.Text = Double.IsNaN(nullPercentage) ? "" : nullPercentage.ToString();
393      lblValues.Text = totalValuesCount.ToString();
394      lblMinimum.Text = minimum == null ? "" : minimum.ToString();
395      lblMaximum.Text = maximum == null ? "" : maximum.ToString();
396      lblMean.Text = mean == null ? "" : mean.ToString();
397      lblStdDev.Text = stddev == null ? "" : stddev.ToString();
398      lblMedian.Text = median == null ? "" : median.ToString();
399      UpdateStateInformation(true);
400    }
401
402    private void dataGridView_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e) {
403      RecalculateWidthOfControl();
404    }
405
406    private void RecalculateWidthOfControl() {
407      int width = 0;
408      foreach (DataGridViewColumn col in this.dataGridView.Columns)
409        width += col.Width;
410      //no columns in datagrid
411      if (width != 0) {
412        width += this.dataGridView.RowHeadersWidth;
413        //datagridview.controls[1] is always the vertical scrollbar
414        if (dataGridView.Controls[1].Visible)
415          width += 20;
416      }
417      this.dataGridView.Width = width;
418      this.Width = width;
419    }
420
421    private void dataGridView_MouseClick(object sender, MouseEventArgs e) {
422      this.ColumnGroupActive = true;
423      this.FireActivated(false);
424
425      if (this.dataGridView.AllowUserToOrderColumns || e.Clicks >= 2)
426        return;
427
428      System.Windows.Forms.DataGridView.HitTestInfo hit = dataGridView.HitTest(e.X, e.Y);
429      // row header click
430      if (hit.ColumnIndex == -1 && hit.RowIndex >= 0) {
431        if (e.Button == MouseButtons.Right) {
432          this.commandChain.Add(new InsertRowCommand(DataSet, this.ColumnGroup.Name, hit.RowIndex));
433        } else {
434          if (dataGridView.SelectionMode != DataGridViewSelectionMode.RowHeaderSelect)
435            dataGridView.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
436        }
437      } // column header click
438      else if (hit.RowIndex == -1 && hit.ColumnIndex >= 0 && e.Button == MouseButtons.Left) {
439        if (dataGridView.SelectionMode != DataGridViewSelectionMode.ColumnHeaderSelect)
440          dataGridView.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
441      }
442    }
443
444    private void dataGridView_AllowUserToOrderColumnsChanged(object sender, EventArgs e) {
445      if (this.dataGridView.AllowUserToOrderColumns)
446        this.dataGridView.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
447    }
448
449    private void dataGridView_KeyDown(object sender, KeyEventArgs e) {
450      //handle deletion of columns and emptying of cells
451      if (e.KeyCode == Keys.Delete) {
452        if (this.dataGridView.SelectedColumns.Count != 0)
453          this.commandChain.Add(new DeleteColumnCommand(DataSet, this.ColumnGroup.Name, ColumnGroup.SelectedColumnIndexes));
454        else if (this.dataGridView.SelectedRows.Count != 0) {
455          //to ensure that cells are not emptied before the rows got deleted
456          //deleting of rows handled by user deleting rows
457        } else if (this.dataGridView.SelectedCells.Count != 0) {
458          List<Point> cells = new List<Point>();
459          foreach (DataGridViewCell cell in this.dataGridView.SelectedCells) {
460            if (cell.RowIndex < this.ColumnGroup.Columns.ElementAt(cell.ColumnIndex).TotalValuesCount && cell.Value != null)
461              cells.Add(new Point(cell.ColumnIndex, cell.RowIndex));
462          }
463          if (cells.Count != 0)
464            this.commandChain.Add(new ChangeValuesToNullCommand(DataSet, this.ColumnGroup.Name, cells));
465        }
466      }
467        //handle paste of values
468      else if (e.Control && e.KeyCode == Keys.V)
469        PasteClipboardContent();
470      else if (e.Control && e.KeyCode == Keys.C)
471        CopyClipboardContent();
472    }
473
474    private void dataGridView_RowHeadersWidthChanged(object sender, EventArgs e) {
475      this.RecalculateWidthOfControl();
476    }
477
478    private void dataGridView_CellMouseEnter(object sender, DataGridViewCellEventArgs e) {
479      if (e.RowIndex == -1 && e.ColumnIndex != -1) {
480        toolTip.SetToolTip(this.dataGridView, ToExcelColumnIndex(e.ColumnIndex));
481      } else if (toolTip.Active) {
482        toolTip.RemoveAll();
483      }
484    }
485
486    private void dataGridView_Resize(object sender, EventArgs e) {
487      UpdateDataGridViewHeaderCells();
488    }
489
490    private void dataGridView_Scroll(object sender, ScrollEventArgs e) {
491      if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
492        UpdateDataGridViewHeaderCells();
493    }
494
495    private string ToExcelColumnIndex(int index) {
496      #region digits
497      var digits = new char[] {
498        '#',
499        'A',
500        'B',
501        'C',
502        'D',
503        'E',
504        'F',
505        'G',
506        'H',
507        'I',
508        'J',
509        'K',
510        'L',
511        'M',
512        'N',
513        'O',
514        'P',
515        'Q',
516        'R',
517        'S',
518        'T',
519        'U',
520        'V',
521        'W',
522        'X',
523        'Y',
524        'Z'};
525      int b = digits.Length - 1;
526      #endregion
527      string excelIndex = string.Empty;
528      index = index + 1;
529      while (index > 0) {
530        int d = index / b;
531        int rem = index % b;
532        index = d;
533        excelIndex = digits[rem] + excelIndex;
534      }
535      return excelIndex;
536    }
537
538    #region DataGridView virtual mode event handler
539    private void dataGridView_CellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e) {
540      if (e.ColumnIndex >= ColumnGroup.Columns.Count()) return;
541      if (e.RowIndex >= ColumnGroup.RowCount) return;
542      e.Value = ColumnGroup.Columns.ElementAt(e.ColumnIndex).GetValue(e.RowIndex);
543    }
544
545    private void dataGridView_CellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e) {
546      IComparable value = null;
547      try {
548        if (e.Value == null)
549          value = null;
550        else if (this.ColumnGroup.Columns.ElementAt(e.ColumnIndex).DataType == typeof(double?))
551          value = double.Parse((string)e.Value);
552        else if (this.ColumnGroup.Columns.ElementAt(e.ColumnIndex).DataType == typeof(DateTime?))
553          value = DateTime.Parse((string)e.Value);
554        else
555          value = e.Value.ToString();
556      }
557      catch (FormatException) {
558      }
559
560      if (e.RowIndex == this.dataGridView.RowCount - 1) {
561        IComparable[] row = new IComparable[this.ColumnGroup.Columns.Count()];
562        row[e.ColumnIndex] = value;
563        this.commandChain.Add(new AddRowCommand(this.DataSet, this.ColumnGroup.Name, row));
564      } else {
565        this.commandChain.Add(new ChangeValueCommand(this.DataSet, this.ColumnGroup.Name, e.ColumnIndex, e.RowIndex, value));
566      }
567    }
568
569    private void dataGridView_UserDeletingRow(object sender, System.Windows.Forms.DataGridViewRowCancelEventArgs e) {
570      e.Cancel = true;
571      if (e.Row.Index < this.ColumnGroup.RowCount) {
572        List<int> positions;
573        if (this.dataGridView.AreAllCellsSelected(true))
574          positions = Enumerable.Range(0, this.ColumnGroup.RowCount).ToList();
575        else {
576          positions = new List<int>();
577          for (int i = 0; i < this.dataGridView.SelectedRows.Count; i++)
578            if (this.dataGridView.SelectedRows[i].Index < this.ColumnGroup.RowCount)
579              positions.Add(this.dataGridView.SelectedRows[i].Index);
580          positions.Sort();
581        }
582        if (positions.Count != 0) {
583          this.commandChain.Add(new DeleteRowsCommand(DataSet, this.ColumnGroup.Name, positions));
584        }
585        this.dataGridView.ClearSelection();
586      }
587    }
588    #endregion
589
590    #region txtColumnName event handler
591    private void txtColumnName_Validated(object source, EventArgs e) {
592      if (!this.txtColumnName.Visible) return;
593      this.txtColumnName.Visible = false;
594
595      DataGridView.HitTestInfo h = this.dataGridView.HitTest(txtColumnName.Location.X, txtColumnName.Location.Y);
596      if (txtColumnName.Text != this.ColumnGroup.GetColumn(h.ColumnIndex).Name)
597        this.commandChain.Add(new RenameColumnCommand(DataSet, this.ColumnGroup.Name, h.ColumnIndex, txtColumnName.Text));
598    }
599
600    private void txtColumnName_KeyDown(object source, KeyEventArgs e) {
601      if (e.KeyCode == Keys.Escape) {
602        DataGridView.HitTestInfo h = this.dataGridView.HitTest(txtColumnName.Location.X, txtColumnName.Location.Y);
603        this.txtColumnName.Text = this.ColumnGroup.GetColumn(h.ColumnIndex).Name;
604        dataGridView.Select();
605      } else if (e.KeyCode == Keys.Enter)
606        dataGridView.Select();
607    }
608    #endregion
609
610    #region txtColumnGroupName event handler
611    private void txtColumnGroupName_Click(object sender, EventArgs e) {
612      bool ctrlPressed = (Control.ModifierKeys & Keys.Control) == Keys.Control;
613      this.dataGridView.ClearSelection();
614      this.ColumnGroupActive = !this.ColumnGroupActive;
615      FireActivated(ctrlPressed);
616      UpdateStateInformation();
617    }
618
619    private void txtColumnGroupName_DoubleClick(object sender, EventArgs e) {
620      txtColumnGroupName.ReadOnly = false;
621      txtColumnGroupName.Text = ColumnGroup.Name;
622    }
623
624    private void txtColumnGroupName_Leave(object sender, EventArgs e) {
625      if (!txtColumnGroupName.ReadOnly) {
626        txtColumnGroupName.ReadOnly = true;
627        if (CheckIfNewColumnGroupNameIsAllowed(txtColumnGroupName.Text))
628          this.commandChain.Add(new RenameColumnGroupCommand(DataSet, this.ColumnGroup.Name, txtColumnGroupName.Text));
629      }
630    }
631
632    private void txtColumnGroupName_KeyDown(object sender, KeyEventArgs e) {
633      if (this.txtColumnGroupName.ReadOnly) //user can not change the name if it is readonly
634        return;
635      if (e.KeyCode != Keys.Enter && e.KeyCode != Keys.Escape)
636        return;
637      if (e.KeyCode == Keys.Enter && CheckIfNewColumnGroupNameIsAllowed(txtColumnGroupName.Text)) {
638        this.commandChain.Add(new RenameColumnGroupCommand(DataSet, this.ColumnGroup.Name, txtColumnGroupName.Text));
639      }
640      txtColumnGroupName.ReadOnly = true;
641      this.Focus();
642      this.Refresh();
643    }
644
645    private bool CheckIfNewColumnGroupNameIsAllowed(string newName) {
646      return this.DataSet.ColumnGroups.All(cg => cg.Name != newName);
647    }
648    #endregion
649  }
650}
Note: See TracBrowser for help on using the repository browser.