Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7766 was 7731, checked in by sforsten, 13 years ago

#1818: The previous solution didn't work out.
Similar to the StringConvertibleMatrixView the ColumnGroupView now only updates the row headers which are displayed.

File size: 26.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Leave += new EventHandler(txtColumnName_Leave);
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        //this.UpdateSortGlyph();
336        //this.dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
337        //UpdateStateInformation();
338      }
339    }
340
341    private void dataGridView_ColumnHeaderMouseDoubleClick(object sender, System.Windows.Forms.DataGridViewCellMouseEventArgs e) {
342      if (e.Button == MouseButtons.Left) {
343        dataGridView.ClearSelection();
344        Rectangle rect = dataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
345        ColumnBase col = this.ColumnGroup.Columns.ElementAt(e.ColumnIndex);
346        txtColumnName.Text = col.Name;
347        txtColumnName.Size = rect.Size;
348        txtColumnName.Location = rect.Location;
349        txtColumnName.Visible = true;
350        txtColumnName.Focus();
351        txtColumnName.BringToFront();
352      }
353    }
354
355    private void UpdateSortGlyph() {
356      System.Collections.IEnumerator e = this.dataGridView.Columns.GetEnumerator();
357      e.MoveNext();
358      foreach (SortOrder sortOrder in this.ColumnGroup.SortOrdersForColumns) {
359        ((DataGridViewColumn)e.Current).HeaderCell.SortGlyphDirection = sortOrder;
360        e.MoveNext();
361      }
362    }
363
364    private void dataGridView_SelectionChanged(object sender, EventArgs e) {
365      int nullValuesCount = 0;
366      int totalValuesCount = 0;
367      IComparable minimum = "";
368      IComparable maximum = "";
369      double? mean = null;
370      double? median = null;
371      double? stddev = null;
372      ColumnBase column;
373
374      this.ColumnGroup.ClearSelectedColumns();
375      if (this.dataGridView.SelectedColumns.Count != 0) {
376        foreach (DataGridViewColumn col in this.dataGridView.SelectedColumns) {
377          column = this.ColumnGroup.Columns.ElementAt(col.Index);
378          column.Selected = true;
379          nullValuesCount += column.NullValuesCount;
380          totalValuesCount += column.NonNullValuesCount;
381        }
382        if (this.dataGridView.SelectedColumns.Count == 1) {
383          column = this.ColumnGroup.Columns.ElementAt(dataGridView.SelectedColumns[0].Index);
384          minimum = column.Minimum;
385          maximum = column.Maximum;
386          if (column is DoubleColumn) {
387            mean = ((DoubleColumn)column).Mean;
388            stddev = ((DoubleColumn)column).StandardDeviation;
389            median = ((DoubleColumn)column).Median;
390          }
391        }
392      }
393      lblNullValues.Text = nullValuesCount.ToString();
394      lblValues.Text = totalValuesCount.ToString();
395      lblMinimum.Text = minimum == null ? "" : minimum.ToString();
396      lblMaximum.Text = maximum == null ? "" : maximum.ToString();
397      lblMean.Text = mean == null ? "" : mean.ToString();
398      lblStdDev.Text = stddev == null ? "" : stddev.ToString();
399      lblMedian.Text = median == null ? "" : median.ToString();
400      UpdateStateInformation(true);
401    }
402
403    private void dataGridView_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e) {
404      RecalculateWidthOfControl();
405    }
406
407    private void RecalculateWidthOfControl() {
408      int width = 0;
409      foreach (DataGridViewColumn col in this.dataGridView.Columns)
410        width += col.Width;
411      //no columns in datagrid
412      if (width != 0) {
413        width += this.dataGridView.RowHeadersWidth;
414        //datagridview.controls[1] is always the vertical scrollbar
415        if (dataGridView.Controls[1].Visible)
416          width += 20;
417      }
418      this.dataGridView.Width = width;
419      this.Width = width;
420    }
421
422    private void dataGridView_MouseClick(object sender, MouseEventArgs e) {
423      this.ColumnGroupActive = true;
424      this.FireActivated(false);
425
426      if (this.dataGridView.AllowUserToOrderColumns || e.Clicks >= 2)
427        return;
428
429      System.Windows.Forms.DataGridView.HitTestInfo hit = dataGridView.HitTest(e.X, e.Y);
430      // row header click
431      if (hit.ColumnIndex == -1 && hit.RowIndex >= 0) {
432        if (e.Button == MouseButtons.Right) {
433          this.commandChain.Add(new InsertRowCommand(DataSet, this.ColumnGroup.Name, hit.RowIndex));
434        } else {
435          if (dataGridView.SelectionMode != DataGridViewSelectionMode.RowHeaderSelect)
436            dataGridView.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
437        }
438      } // column header click
439      else if (hit.RowIndex == -1 && hit.ColumnIndex >= 0 && e.Button == MouseButtons.Left) {
440        if (dataGridView.SelectionMode != DataGridViewSelectionMode.ColumnHeaderSelect)
441          dataGridView.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
442      }
443    }
444
445    private void dataGridView_AllowUserToOrderColumnsChanged(object sender, EventArgs e) {
446      if (this.dataGridView.AllowUserToOrderColumns)
447        this.dataGridView.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
448    }
449
450    private void dataGridView_KeyDown(object sender, KeyEventArgs e) {
451      //handle deletion of columns and emptying of cells
452      if (e.KeyCode == Keys.Delete) {
453        if (this.dataGridView.SelectedColumns.Count != 0)
454          this.commandChain.Add(new DeleteColumnCommand(DataSet, this.ColumnGroup.Name, ColumnGroup.SelectedColumnIndexes));
455        else if (this.dataGridView.SelectedRows.Count != 0) {
456          //to ensure that cells are not emptied before the rows got deleted
457          //deleting of rows handled by user deleting rows
458        } else if (this.dataGridView.SelectedCells.Count != 0) {
459          List<Point> cells = new List<Point>();
460          foreach (DataGridViewCell cell in this.dataGridView.SelectedCells) {
461            if (cell.RowIndex < this.ColumnGroup.Columns.ElementAt(cell.ColumnIndex).TotalValuesCount && cell.Value != null)
462              cells.Add(new Point(cell.ColumnIndex, cell.RowIndex));
463          }
464          if (cells.Count != 0)
465            this.commandChain.Add(new ChangeValuesToNullCommand(DataSet, this.ColumnGroup.Name, cells));
466        }
467      }
468        //handle paste of values
469      else if (e.Control && e.KeyCode == Keys.V)
470        PasteClipboardContent();
471      else if (e.Control && e.KeyCode == Keys.C)
472        CopyClipboardContent();
473    }
474
475    private void dataGridView_RowHeadersWidthChanged(object sender, EventArgs e) {
476      this.RecalculateWidthOfControl();
477    }
478
479    private void dataGridView_CellMouseEnter(object sender, DataGridViewCellEventArgs e) {
480      if (e.RowIndex == -1 && e.ColumnIndex != -1) {
481        toolTip.SetToolTip(this.dataGridView, ToExcelColumnIndex(e.ColumnIndex));
482      } else {
483        toolTip.Hide(this.dataGridView);
484      }
485    }
486
487    private void dataGridView_Resize(object sender, EventArgs e) {
488      UpdateDataGridViewHeaderCells();
489    }
490
491    private void dataGridView_Scroll(object sender, ScrollEventArgs e) {
492      if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
493        UpdateDataGridViewHeaderCells();
494    }
495
496    private string ToExcelColumnIndex(int index) {
497      //if (symb.Length == 1) {       // 'A' .. 'Z'
498      //  return TryTranslateColumnIndexDigit(symb[0], out columnIndex);
499      //} else if (symb.Length == 2) { // 'AA' ... 'ZZ'
500      //  bool ok;
501      //  int d0, d1;
502      //  ok = TryTranslateColumnIndexDigit(symb[0], out d1) & TryTranslateColumnIndexDigit(symb[1], out d0);
503      //  columnIndex = (d1 + 1) * 26 + d0;
504      //  return ok;
505      //} else {
506      //  columnIndex = 0;
507      //  return false;
508      //}
509      #region digits
510      var digits = new char[] {
511        '#',
512        'A',
513        'B',
514        'C',
515        'D',
516        'E',
517        'F',
518        'G',
519        'H',
520        'I',
521        'J',
522        'K',
523        'L',
524        'M',
525        'N',
526        'O',
527        'P',
528        'Q',
529        'R',
530        'S',
531        'T',
532        'U',
533        'V',
534        'W',
535        'X',
536        'Y',
537        'Z'};
538      int b = digits.Length - 1;
539      #endregion
540      string excelIndex = string.Empty;
541      index = index + 1;
542      while (index > 0) {
543        int d = index / b;
544        int rem = index % b;
545        index = d;
546        excelIndex = digits[rem] + excelIndex;
547      }
548      return excelIndex;
549    }
550
551    #region DataGridView virtual mode event handler
552    private void dataGridView_CellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e) {
553      e.Value = ColumnGroup.Columns.ElementAt(e.ColumnIndex).GetValue(e.RowIndex);
554    }
555
556    private void dataGridView_CellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e) {
557      IComparable value = null;
558      try {
559        if (e.Value == null)
560          value = null;
561        else if (this.ColumnGroup.Columns.ElementAt(e.ColumnIndex).DataType == typeof(double?))
562          value = double.Parse((string)e.Value);
563        else if (this.ColumnGroup.Columns.ElementAt(e.ColumnIndex).DataType == typeof(DateTime?))
564          value = DateTime.Parse((string)e.Value);
565        else
566          value = e.Value.ToString();
567      }
568      catch (FormatException) {
569      }
570
571      if (e.RowIndex == this.dataGridView.RowCount - 1) {
572        IComparable[] row = new IComparable[this.ColumnGroup.Columns.Count()];
573        row[e.ColumnIndex] = value;
574        this.commandChain.Add(new AddRowCommand(this.DataSet, this.ColumnGroup.Name, row));
575      } else {
576        this.commandChain.Add(new ChangeValueCommand(this.DataSet, this.ColumnGroup.Name, e.ColumnIndex, e.RowIndex, value));
577      }
578    }
579
580    private void dataGridView_UserDeletingRow(object sender, System.Windows.Forms.DataGridViewRowCancelEventArgs e) {
581      e.Cancel = true;
582      if (e.Row.Index < this.ColumnGroup.RowCount) {
583        List<int> positions;
584        if (this.dataGridView.AreAllCellsSelected(true))
585          positions = Enumerable.Range(0, this.ColumnGroup.RowCount).ToList();
586        else {
587          positions = new List<int>();
588          for (int i = 0; i < this.dataGridView.SelectedRows.Count; i++)
589            if (this.dataGridView.SelectedRows[i].Index < this.ColumnGroup.RowCount)
590              positions.Add(this.dataGridView.SelectedRows[i].Index);
591          positions.Sort();
592        }
593        if (positions.Count != 0) {
594          this.commandChain.Add(new DeleteRowsCommand(DataSet, this.ColumnGroup.Name, positions));
595        }
596        this.dataGridView.ClearSelection();
597      }
598    }
599    #endregion
600
601    #region txtColumnName event handler
602    private void txtColumnName_Leave(object source, EventArgs e) {
603      this.txtColumnName.Visible = false;
604    }
605
606    private void txtColumnName_KeyDown(object source, KeyEventArgs e) {
607      if (e.KeyCode != Keys.Enter && e.KeyCode != Keys.Escape)
608        return;
609      if (e.KeyCode == Keys.Enter) {
610        DataGridView.HitTestInfo h = this.dataGridView.HitTest(txtColumnName.Location.X, txtColumnName.Location.Y);
611        this.commandChain.Add(new RenameColumnCommand(DataSet, this.ColumnGroup.Name, h.ColumnIndex, txtColumnName.Text));
612      }
613      this.txtColumnName.Visible = false;
614    }
615    #endregion
616
617    #region txtColumnGroupName event handler
618    private void txtColumnGroupName_Click(object sender, EventArgs e) {
619      bool ctrlPressed = (Control.ModifierKeys & Keys.Control) == Keys.Control;
620      this.dataGridView.ClearSelection();
621      this.ColumnGroupActive = !this.ColumnGroupActive;
622      FireActivated(ctrlPressed);
623      UpdateStateInformation();
624    }
625
626    private void txtColumnGroupName_DoubleClick(object sender, EventArgs e) {
627      txtColumnGroupName.ReadOnly = false;
628      txtColumnGroupName.Text = ColumnGroup.Name;
629    }
630
631    private void txtColumnGroupName_Leave(object sender, EventArgs e) {
632      if (!txtColumnGroupName.ReadOnly) {
633        txtColumnGroupName.ReadOnly = true;
634        if (CheckIfNewColumnGroupNameIsAllowed(txtColumnGroupName.Text))
635          this.commandChain.Add(new RenameColumnGroupCommand(DataSet, this.ColumnGroup.Name, txtColumnGroupName.Text));
636      }
637    }
638
639    private void txtColumnGroupName_KeyDown(object sender, KeyEventArgs e) {
640      if (this.txtColumnGroupName.ReadOnly) //user can not change the name if it is readonly
641        return;
642      if (e.KeyCode != Keys.Enter && e.KeyCode != Keys.Escape)
643        return;
644      if (e.KeyCode == Keys.Enter && CheckIfNewColumnGroupNameIsAllowed(txtColumnGroupName.Text)) {
645        this.commandChain.Add(new RenameColumnGroupCommand(DataSet, this.ColumnGroup.Name, txtColumnGroupName.Text));
646      }
647      txtColumnGroupName.ReadOnly = true;
648      this.Focus();
649      this.Refresh();
650    }
651
652    private bool CheckIfNewColumnGroupNameIsAllowed(string newName) {
653      return !this.DataSet.ColumnGroups.Any(cg => cg.Name == newName);
654    }
655    #endregion
656  }
657}
Note: See TracBrowser for help on using the repository browser.