[2669] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2669] | 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 |
|
---|
| 22 | using System;
|
---|
[4068] | 23 | using System.Collections.Generic;
|
---|
[2669] | 24 | using System.ComponentModel;
|
---|
[4652] | 25 | using System.Drawing;
|
---|
[3312] | 26 | using System.Linq;
|
---|
[4178] | 27 | using System.Text;
|
---|
[2669] | 28 | using System.Windows.Forms;
|
---|
| 29 | using HeuristicLab.Common;
|
---|
| 30 | using HeuristicLab.MainForm;
|
---|
[2713] | 31 | using HeuristicLab.MainForm.WindowsForms;
|
---|
[2669] | 32 |
|
---|
| 33 | namespace HeuristicLab.Data.Views {
|
---|
[3048] | 34 | [View("StringConvertibleMatrix View")]
|
---|
| 35 | [Content(typeof(IStringConvertibleMatrix), true)]
|
---|
[3228] | 36 | public partial class StringConvertibleMatrixView : AsynchronousContentView {
|
---|
[4199] | 37 | private int[] virtualRowIndizes;
|
---|
[3430] | 38 | private List<KeyValuePair<int, SortOrder>> sortedColumnIndizes;
|
---|
| 39 | private RowComparer rowComparer;
|
---|
| 40 |
|
---|
[3048] | 41 | public new IStringConvertibleMatrix Content {
|
---|
| 42 | get { return (IStringConvertibleMatrix)base.Content; }
|
---|
[2713] | 43 | set { base.Content = value; }
|
---|
[2669] | 44 | }
|
---|
| 45 |
|
---|
[6685] | 46 | public DataGridView DataGridView {
|
---|
| 47 | get { return dataGridView; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[3430] | 50 | public override bool ReadOnly {
|
---|
| 51 | get {
|
---|
| 52 | if ((Content != null) && Content.ReadOnly) return true;
|
---|
| 53 | return base.ReadOnly;
|
---|
| 54 | }
|
---|
| 55 | set { base.ReadOnly = value; }
|
---|
| 56 | }
|
---|
[3314] | 57 |
|
---|
[4652] | 58 | private bool showRowsAndColumnsTextBox;
|
---|
| 59 | public bool ShowRowsAndColumnsTextBox {
|
---|
| 60 | get { return showRowsAndColumnsTextBox; }
|
---|
| 61 | set {
|
---|
[4709] | 62 | showRowsAndColumnsTextBox = value;
|
---|
| 63 | UpdateVisibilityOfTextBoxes();
|
---|
[4652] | 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[4709] | 67 | private bool showStatisticalInformation;
|
---|
| 68 | public bool ShowStatisticalInformation {
|
---|
| 69 | get { return showStatisticalInformation; }
|
---|
| 70 | set {
|
---|
| 71 | showStatisticalInformation = value;
|
---|
| 72 | UpdateVisibilityOfStatisticalInformation();
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[3048] | 76 | public StringConvertibleMatrixView() {
|
---|
[2669] | 77 | InitializeComponent();
|
---|
[4709] | 78 | ShowRowsAndColumnsTextBox = true;
|
---|
[4720] | 79 | ShowStatisticalInformation = true;
|
---|
[2677] | 80 | errorProvider.SetIconAlignment(rowsTextBox, ErrorIconAlignment.MiddleLeft);
|
---|
| 81 | errorProvider.SetIconPadding(rowsTextBox, 2);
|
---|
| 82 | errorProvider.SetIconAlignment(columnsTextBox, ErrorIconAlignment.MiddleLeft);
|
---|
| 83 | errorProvider.SetIconPadding(columnsTextBox, 2);
|
---|
[3314] | 84 | sortedColumnIndizes = new List<KeyValuePair<int, SortOrder>>();
|
---|
| 85 | rowComparer = new RowComparer();
|
---|
[2669] | 86 | }
|
---|
| 87 |
|
---|
[2713] | 88 | protected override void DeregisterContentEvents() {
|
---|
| 89 | Content.ItemChanged -= new EventHandler<EventArgs<int, int>>(Content_ItemChanged);
|
---|
| 90 | Content.Reset -= new EventHandler(Content_Reset);
|
---|
[3320] | 91 | Content.ColumnNamesChanged -= new EventHandler(Content_ColumnNamesChanged);
|
---|
| 92 | Content.RowNamesChanged -= new EventHandler(Content_RowNamesChanged);
|
---|
[2713] | 93 | base.DeregisterContentEvents();
|
---|
[2669] | 94 | }
|
---|
[2713] | 95 | protected override void RegisterContentEvents() {
|
---|
| 96 | base.RegisterContentEvents();
|
---|
| 97 | Content.ItemChanged += new EventHandler<EventArgs<int, int>>(Content_ItemChanged);
|
---|
| 98 | Content.Reset += new EventHandler(Content_Reset);
|
---|
[3320] | 99 | Content.ColumnNamesChanged += new EventHandler(Content_ColumnNamesChanged);
|
---|
| 100 | Content.RowNamesChanged += new EventHandler(Content_RowNamesChanged);
|
---|
[2669] | 101 | }
|
---|
| 102 |
|
---|
[2713] | 103 | protected override void OnContentChanged() {
|
---|
| 104 | base.OnContentChanged();
|
---|
| 105 | if (Content == null) {
|
---|
[2677] | 106 | rowsTextBox.Text = "";
|
---|
| 107 | columnsTextBox.Text = "";
|
---|
[2669] | 108 | dataGridView.Rows.Clear();
|
---|
[2677] | 109 | dataGridView.Columns.Clear();
|
---|
[3324] | 110 | virtualRowIndizes = new int[0];
|
---|
[3936] | 111 | } else
|
---|
[2713] | 112 | UpdateData();
|
---|
[3904] | 113 | }
|
---|
[3764] | 114 |
|
---|
[3904] | 115 | protected override void SetEnabledStateOfControls() {
|
---|
| 116 | base.SetEnabledStateOfControls();
|
---|
[3454] | 117 | rowsTextBox.Enabled = Content != null;
|
---|
| 118 | columnsTextBox.Enabled = Content != null;
|
---|
| 119 | dataGridView.Enabled = Content != null;
|
---|
| 120 | rowsTextBox.ReadOnly = ReadOnly;
|
---|
| 121 | columnsTextBox.ReadOnly = ReadOnly;
|
---|
| 122 | dataGridView.ReadOnly = ReadOnly;
|
---|
[3350] | 123 | }
|
---|
[2669] | 124 |
|
---|
[2713] | 125 | private void UpdateData() {
|
---|
| 126 | rowsTextBox.Text = Content.Rows.ToString();
|
---|
[2973] | 127 | rowsTextBox.Enabled = true;
|
---|
[2713] | 128 | columnsTextBox.Text = Content.Columns.ToString();
|
---|
[2973] | 129 | columnsTextBox.Enabled = true;
|
---|
[4850] | 130 | virtualRowIndizes = Enumerable.Range(0, Content.Rows).ToArray();
|
---|
[4707] | 131 |
|
---|
[4850] | 132 | if (Content.Columns == 0 && dataGridView.ColumnCount != Content.Columns && !Content.ReadOnly)
|
---|
| 133 | Content.Columns = dataGridView.ColumnCount;
|
---|
| 134 | else {
|
---|
| 135 | DataGridViewColumn[] columns = new DataGridViewColumn[Content.Columns];
|
---|
| 136 | for (int i = 0; i < columns.Length; ++i) {
|
---|
| 137 | var column = new DataGridViewTextBoxColumn();
|
---|
| 138 | column.FillWeight = 1;
|
---|
| 139 | columns[i] = column;
|
---|
| 140 | }
|
---|
| 141 | dataGridView.Columns.Clear();
|
---|
| 142 | dataGridView.Columns.AddRange(columns);
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[4178] | 145 | //DataGridViews with rows but no columns are not allowed !
|
---|
[3431] | 146 | if (Content.Rows == 0 && dataGridView.RowCount != Content.Rows && !Content.ReadOnly)
|
---|
[3335] | 147 | Content.Rows = dataGridView.RowCount;
|
---|
| 148 | else
|
---|
| 149 | dataGridView.RowCount = Content.Rows;
|
---|
[4798] | 150 |
|
---|
[4850] | 151 |
|
---|
[4523] | 152 | ClearSorting();
|
---|
[4199] | 153 | UpdateColumnHeaders();
|
---|
[3328] | 154 | UpdateRowHeaders();
|
---|
[4707] | 155 |
|
---|
[4199] | 156 | dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
|
---|
| 157 | dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
|
---|
[2669] | 158 | dataGridView.Enabled = true;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[4819] | 161 | protected virtual void UpdateColumnHeaders() {
|
---|
[4798] | 162 | HashSet<string> invisibleColumnNames = new HashSet<string>(dataGridView.Columns.OfType<DataGridViewColumn>()
|
---|
| 163 | .Where(c => !c.Visible && !string.IsNullOrEmpty(c.HeaderText)).Select(c => c.HeaderText));
|
---|
[4707] | 164 |
|
---|
[4178] | 165 | for (int i = 0; i < dataGridView.ColumnCount; i++) {
|
---|
[4707] | 166 | if (i < Content.ColumnNames.Count())
|
---|
[3312] | 167 | dataGridView.Columns[i].HeaderText = Content.ColumnNames.ElementAt(i);
|
---|
| 168 | else
|
---|
[3346] | 169 | dataGridView.Columns[i].HeaderText = "Column " + (i + 1);
|
---|
[4798] | 170 | dataGridView.Columns[i].Visible = !invisibleColumnNames.Contains(dataGridView.Columns[i].HeaderText);
|
---|
[3312] | 171 | }
|
---|
| 172 | }
|
---|
[4199] | 173 | protected void UpdateRowHeaders() {
|
---|
| 174 | int index = dataGridView.FirstDisplayedScrollingRowIndex;
|
---|
| 175 | if (index == -1) index = 0;
|
---|
| 176 | int updatedRows = 0;
|
---|
| 177 | int count = dataGridView.DisplayedRowCount(true);
|
---|
[3312] | 178 |
|
---|
[4199] | 179 | while (updatedRows < count) {
|
---|
[4707] | 180 | if (virtualRowIndizes[index] < Content.RowNames.Count())
|
---|
[4199] | 181 | dataGridView.Rows[index].HeaderCell.Value = Content.RowNames.ElementAt(virtualRowIndizes[index]);
|
---|
[3314] | 182 | else
|
---|
[4199] | 183 | dataGridView.Rows[index].HeaderCell.Value = "Row " + (index + 1);
|
---|
| 184 | if (dataGridView.Rows[index].Visible)
|
---|
| 185 | updatedRows++;
|
---|
| 186 | index++;
|
---|
[3312] | 187 | }
|
---|
| 188 | }
|
---|
| 189 |
|
---|
[3320] | 190 | private void Content_RowNamesChanged(object sender, EventArgs e) {
|
---|
[3328] | 191 | if (InvokeRequired)
|
---|
| 192 | Invoke(new EventHandler(Content_RowNamesChanged), sender, e);
|
---|
| 193 | else
|
---|
| 194 | UpdateRowHeaders();
|
---|
[3320] | 195 | }
|
---|
| 196 | private void Content_ColumnNamesChanged(object sender, EventArgs e) {
|
---|
[3328] | 197 | if (InvokeRequired)
|
---|
| 198 | Invoke(new EventHandler(Content_ColumnNamesChanged), sender, e);
|
---|
| 199 | else
|
---|
| 200 | UpdateColumnHeaders();
|
---|
[3320] | 201 | }
|
---|
[2713] | 202 | private void Content_ItemChanged(object sender, EventArgs<int, int> e) {
|
---|
[2669] | 203 | if (InvokeRequired)
|
---|
[2713] | 204 | Invoke(new EventHandler<EventArgs<int, int>>(Content_ItemChanged), sender, e);
|
---|
[3328] | 205 | else
|
---|
[3335] | 206 | dataGridView.InvalidateCell(e.Value2, e.Value);
|
---|
[2669] | 207 | }
|
---|
[2713] | 208 | private void Content_Reset(object sender, EventArgs e) {
|
---|
[2669] | 209 | if (InvokeRequired)
|
---|
[2713] | 210 | Invoke(new EventHandler(Content_Reset), sender, e);
|
---|
[2669] | 211 | else
|
---|
[2713] | 212 | UpdateData();
|
---|
[2669] | 213 | }
|
---|
| 214 |
|
---|
[2677] | 215 | #region TextBox Events
|
---|
| 216 | private void rowsTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
[3714] | 217 | if (ReadOnly || Locked)
|
---|
| 218 | return;
|
---|
[2669] | 219 | int i = 0;
|
---|
[3335] | 220 | if (!int.TryParse(rowsTextBox.Text, out i) || (i <= 0)) {
|
---|
[2676] | 221 | e.Cancel = true;
|
---|
[3335] | 222 | errorProvider.SetError(rowsTextBox, "Invalid Number of Rows (Valid values are positive integers larger than 0)");
|
---|
[2677] | 223 | rowsTextBox.SelectAll();
|
---|
[2669] | 224 | }
|
---|
| 225 | }
|
---|
[2677] | 226 | private void rowsTextBox_Validated(object sender, EventArgs e) {
|
---|
[3430] | 227 | if (!Content.ReadOnly) Content.Rows = int.Parse(rowsTextBox.Text);
|
---|
[2677] | 228 | errorProvider.SetError(rowsTextBox, string.Empty);
|
---|
[2669] | 229 | }
|
---|
[2677] | 230 | private void rowsTextBox_KeyDown(object sender, KeyEventArgs e) {
|
---|
[2669] | 231 | if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
|
---|
[2677] | 232 | rowsLabel.Focus(); // set focus on label to validate data
|
---|
[2676] | 233 | if (e.KeyCode == Keys.Escape) {
|
---|
[2973] | 234 | rowsTextBox.Text = Content.Rows.ToString();
|
---|
[2677] | 235 | rowsLabel.Focus(); // set focus on label to validate data
|
---|
[2676] | 236 | }
|
---|
[2669] | 237 | }
|
---|
[2677] | 238 | private void columnsTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
[3714] | 239 | if (ReadOnly || Locked)
|
---|
| 240 | return;
|
---|
[2677] | 241 | int i = 0;
|
---|
[3335] | 242 | if (!int.TryParse(columnsTextBox.Text, out i) || (i <= 0)) {
|
---|
[2677] | 243 | e.Cancel = true;
|
---|
[3335] | 244 | errorProvider.SetError(columnsTextBox, "Invalid Number of Columns (Valid values are positive integers larger than 0)");
|
---|
[2677] | 245 | columnsTextBox.SelectAll();
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 | private void columnsTextBox_Validated(object sender, EventArgs e) {
|
---|
[3430] | 249 | if (!Content.ReadOnly) Content.Columns = int.Parse(columnsTextBox.Text);
|
---|
[2677] | 250 | errorProvider.SetError(columnsTextBox, string.Empty);
|
---|
| 251 | }
|
---|
| 252 | private void columnsTextBox_KeyDown(object sender, KeyEventArgs e) {
|
---|
| 253 | if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
|
---|
| 254 | columnsLabel.Focus(); // set focus on label to validate data
|
---|
| 255 | if (e.KeyCode == Keys.Escape) {
|
---|
[2713] | 256 | columnsTextBox.Text = Content.Columns.ToString();
|
---|
[2677] | 257 | columnsLabel.Focus(); // set focus on label to validate data
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 | #endregion
|
---|
| 261 |
|
---|
| 262 | #region DataGridView Events
|
---|
[2669] | 263 | private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
|
---|
[3328] | 264 | if (!dataGridView.ReadOnly) {
|
---|
| 265 | string errorMessage;
|
---|
[4030] | 266 | if (Content != null && !Content.Validate(e.FormattedValue.ToString(), out errorMessage)) {
|
---|
[3328] | 267 | e.Cancel = true;
|
---|
| 268 | dataGridView.Rows[e.RowIndex].ErrorText = errorMessage;
|
---|
| 269 | }
|
---|
[2676] | 270 | }
|
---|
[2669] | 271 | }
|
---|
[2676] | 272 | private void dataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) {
|
---|
[3328] | 273 | if (!dataGridView.ReadOnly) {
|
---|
| 274 | string value = e.Value.ToString();
|
---|
| 275 | int rowIndex = virtualRowIndizes[e.RowIndex];
|
---|
| 276 | e.ParsingApplied = Content.SetValue(value, rowIndex, e.ColumnIndex);
|
---|
| 277 | if (e.ParsingApplied) e.Value = Content.GetValue(rowIndex, e.ColumnIndex);
|
---|
| 278 | }
|
---|
[2669] | 279 | }
|
---|
[2676] | 280 | private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
|
---|
| 281 | dataGridView.Rows[e.RowIndex].ErrorText = string.Empty;
|
---|
| 282 | }
|
---|
[3312] | 283 | private void dataGridView_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e) {
|
---|
[4011] | 284 | if (Content != null && e.RowIndex < Content.Rows && e.ColumnIndex < Content.Columns) {
|
---|
[3337] | 285 | int rowIndex = virtualRowIndizes[e.RowIndex];
|
---|
[3335] | 286 | e.Value = Content.GetValue(rowIndex, e.ColumnIndex);
|
---|
[3337] | 287 | }
|
---|
[3312] | 288 | }
|
---|
[4178] | 289 |
|
---|
| 290 | private void dataGridView_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e) {
|
---|
| 291 | this.UpdateRowHeaders();
|
---|
[3312] | 292 | }
|
---|
| 293 | private void dataGridView_Resize(object sender, EventArgs e) {
|
---|
[4178] | 294 | this.UpdateRowHeaders();
|
---|
[3312] | 295 | }
|
---|
[3314] | 296 |
|
---|
[3643] | 297 | private void dataGridView_KeyDown(object sender, KeyEventArgs e) {
|
---|
[4178] | 298 | if (!ReadOnly && e.Control && e.KeyCode == Keys.V)
|
---|
| 299 | PasteValuesToDataGridView();
|
---|
| 300 | else if (e.Control && e.KeyCode == Keys.C)
|
---|
| 301 | CopyValuesFromDataGridView();
|
---|
| 302 | }
|
---|
[3643] | 303 |
|
---|
[4178] | 304 | private void CopyValuesFromDataGridView() {
|
---|
| 305 | if (dataGridView.SelectedCells.Count == 0) return;
|
---|
| 306 | StringBuilder s = new StringBuilder();
|
---|
| 307 | int minRowIndex = dataGridView.SelectedCells[0].RowIndex;
|
---|
| 308 | int maxRowIndex = dataGridView.SelectedCells[dataGridView.SelectedCells.Count - 1].RowIndex;
|
---|
| 309 | int minColIndex = dataGridView.SelectedCells[0].ColumnIndex;
|
---|
| 310 | int maxColIndex = dataGridView.SelectedCells[dataGridView.SelectedCells.Count - 1].ColumnIndex;
|
---|
| 311 |
|
---|
| 312 | if (minRowIndex > maxRowIndex) {
|
---|
| 313 | int temp = minRowIndex;
|
---|
| 314 | minRowIndex = maxRowIndex;
|
---|
| 315 | maxRowIndex = temp;
|
---|
| 316 | }
|
---|
| 317 | if (minColIndex > maxColIndex) {
|
---|
| 318 | int temp = minColIndex;
|
---|
| 319 | minColIndex = maxColIndex;
|
---|
| 320 | maxColIndex = temp;
|
---|
| 321 | }
|
---|
| 322 |
|
---|
[4546] | 323 | bool addRowNames = dataGridView.AreAllCellsSelected(false) && Content.RowNames.Count() > 0;
|
---|
| 324 | bool addColumnNames = dataGridView.AreAllCellsSelected(false) && Content.ColumnNames.Count() > 0;
|
---|
[4178] | 325 |
|
---|
| 326 | //add colum names
|
---|
| 327 | if (addColumnNames) {
|
---|
| 328 | if (addRowNames)
|
---|
| 329 | s.Append('\t');
|
---|
| 330 |
|
---|
| 331 | DataGridViewColumn column = dataGridView.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
|
---|
| 332 | while (column != null) {
|
---|
| 333 | s.Append(column.HeaderText);
|
---|
| 334 | s.Append('\t');
|
---|
| 335 | column = dataGridView.Columns.GetNextColumn(column, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
|
---|
[3643] | 336 | }
|
---|
[4178] | 337 | s.Remove(s.Length - 1, 1); //remove last tab
|
---|
| 338 | s.Append(Environment.NewLine);
|
---|
| 339 | }
|
---|
[3643] | 340 |
|
---|
[4178] | 341 | for (int i = minRowIndex; i <= maxRowIndex; i++) {
|
---|
| 342 | int rowIndex = this.virtualRowIndizes[i];
|
---|
| 343 | if (addRowNames) {
|
---|
| 344 | s.Append(Content.RowNames.ElementAt(rowIndex));
|
---|
| 345 | s.Append('\t');
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | DataGridViewColumn column = dataGridView.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
|
---|
| 349 | while (column != null) {
|
---|
| 350 | DataGridViewCell cell = dataGridView[column.Index, i];
|
---|
| 351 | if (cell.Selected) {
|
---|
| 352 | s.Append(Content.GetValue(rowIndex, column.Index));
|
---|
[4600] | 353 | s.Append('\t');
|
---|
[3643] | 354 | }
|
---|
[4600] | 355 |
|
---|
[4178] | 356 | column = dataGridView.Columns.GetNextColumn(column, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
|
---|
[3643] | 357 | }
|
---|
[4178] | 358 | s.Remove(s.Length - 1, 1); //remove last tab
|
---|
| 359 | s.Append(Environment.NewLine);
|
---|
| 360 | }
|
---|
| 361 | Clipboard.SetText(s.ToString());
|
---|
| 362 | }
|
---|
[3643] | 363 |
|
---|
[4178] | 364 | private void PasteValuesToDataGridView() {
|
---|
| 365 | string[,] values = SplitClipboardString(Clipboard.GetText());
|
---|
| 366 | int rowIndex = 0;
|
---|
| 367 | int columnIndex = 0;
|
---|
| 368 | if (dataGridView.CurrentCell != null) {
|
---|
| 369 | rowIndex = dataGridView.CurrentCell.RowIndex;
|
---|
| 370 | columnIndex = dataGridView.CurrentCell.ColumnIndex;
|
---|
[3643] | 371 | }
|
---|
[4178] | 372 |
|
---|
| 373 | for (int row = 0; row < values.GetLength(1); row++) {
|
---|
| 374 | if (row + rowIndex >= Content.Rows)
|
---|
| 375 | Content.Rows = Content.Rows + 1;
|
---|
| 376 | for (int col = 0; col < values.GetLength(0); col++) {
|
---|
| 377 | if (col + columnIndex >= Content.Columns)
|
---|
| 378 | Content.Columns = Content.Columns + 1;
|
---|
| 379 | Content.SetValue(values[col, row], row + rowIndex, col + columnIndex);
|
---|
| 380 | }
|
---|
| 381 | }
|
---|
| 382 | ClearSorting();
|
---|
[3643] | 383 | }
|
---|
| 384 | private string[,] SplitClipboardString(string clipboardText) {
|
---|
| 385 | clipboardText = clipboardText.Remove(clipboardText.Length - Environment.NewLine.Length); //remove last newline constant
|
---|
| 386 | string[,] values = null;
|
---|
| 387 | string[] lines = clipboardText.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
|
---|
| 388 | string[] cells;
|
---|
| 389 | for (int i = 0; i < lines.Length; i++) {
|
---|
| 390 | cells = lines[i].Split('\t');
|
---|
| 391 | if (values == null)
|
---|
| 392 | values = new string[cells.Length, lines.Length];
|
---|
| 393 | for (int j = 0; j < cells.Length; j++)
|
---|
| 394 | values[j, i] = string.IsNullOrEmpty(cells[j]) ? string.Empty : cells[j];
|
---|
| 395 | }
|
---|
| 396 | return values;
|
---|
| 397 | }
|
---|
| 398 |
|
---|
[3314] | 399 | private void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
[3320] | 400 | if (Content != null) {
|
---|
| 401 | if (e.Button == MouseButtons.Left && Content.SortableView) {
|
---|
| 402 | bool addToSortedIndizes = (Control.ModifierKeys & Keys.Control) == Keys.Control;
|
---|
| 403 | SortOrder newSortOrder = SortOrder.Ascending;
|
---|
| 404 | if (sortedColumnIndizes.Any(x => x.Key == e.ColumnIndex)) {
|
---|
| 405 | SortOrder oldSortOrder = sortedColumnIndizes.Where(x => x.Key == e.ColumnIndex).First().Value;
|
---|
| 406 | int enumLength = Enum.GetValues(typeof(SortOrder)).Length;
|
---|
| 407 | newSortOrder = oldSortOrder = (SortOrder)Enum.Parse(typeof(SortOrder), ((((int)oldSortOrder) + 1) % enumLength).ToString());
|
---|
| 408 | }
|
---|
[3314] | 409 |
|
---|
[3320] | 410 | if (!addToSortedIndizes)
|
---|
| 411 | sortedColumnIndizes.Clear();
|
---|
[3314] | 412 |
|
---|
[3320] | 413 | if (sortedColumnIndizes.Any(x => x.Key == e.ColumnIndex)) {
|
---|
| 414 | int sortedIndex = sortedColumnIndizes.FindIndex(x => x.Key == e.ColumnIndex);
|
---|
| 415 | if (newSortOrder != SortOrder.None)
|
---|
| 416 | sortedColumnIndizes[sortedIndex] = new KeyValuePair<int, SortOrder>(e.ColumnIndex, newSortOrder);
|
---|
| 417 | else
|
---|
| 418 | sortedColumnIndizes.RemoveAt(sortedIndex);
|
---|
| 419 | } else
|
---|
| 420 | if (newSortOrder != SortOrder.None)
|
---|
| 421 | sortedColumnIndizes.Add(new KeyValuePair<int, SortOrder>(e.ColumnIndex, newSortOrder));
|
---|
| 422 | Sort();
|
---|
| 423 | }
|
---|
[3314] | 424 | }
|
---|
| 425 | }
|
---|
| 426 |
|
---|
[4518] | 427 | protected virtual void ClearSorting() {
|
---|
[3643] | 428 | virtualRowIndizes = Enumerable.Range(0, Content.Rows).ToArray();
|
---|
| 429 | sortedColumnIndizes.Clear();
|
---|
| 430 | UpdateSortGlyph();
|
---|
| 431 | }
|
---|
| 432 |
|
---|
[3314] | 433 | private void Sort() {
|
---|
[3444] | 434 | virtualRowIndizes = Sort(sortedColumnIndizes);
|
---|
| 435 | UpdateSortGlyph();
|
---|
[3546] | 436 | UpdateRowHeaders();
|
---|
[3444] | 437 | dataGridView.Invalidate();
|
---|
| 438 | }
|
---|
| 439 | protected virtual int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
|
---|
[3328] | 440 | int[] newSortedIndex = Enumerable.Range(0, Content.Rows).ToArray();
|
---|
[3444] | 441 | if (sortedColumns.Count() != 0) {
|
---|
| 442 | rowComparer.SortedIndizes = sortedColumns;
|
---|
[3346] | 443 | rowComparer.Matrix = Content;
|
---|
[3314] | 444 | Array.Sort(newSortedIndex, rowComparer);
|
---|
| 445 | }
|
---|
[3444] | 446 | return newSortedIndex;
|
---|
[3328] | 447 | }
|
---|
| 448 | private void UpdateSortGlyph() {
|
---|
[3314] | 449 | foreach (DataGridViewColumn col in this.dataGridView.Columns)
|
---|
| 450 | col.HeaderCell.SortGlyphDirection = SortOrder.None;
|
---|
| 451 | foreach (KeyValuePair<int, SortOrder> p in sortedColumnIndizes)
|
---|
| 452 | this.dataGridView.Columns[p.Key].HeaderCell.SortGlyphDirection = p.Value;
|
---|
| 453 | }
|
---|
[2677] | 454 | #endregion
|
---|
[3312] | 455 |
|
---|
[3346] | 456 | public class RowComparer : IComparer<int> {
|
---|
[3314] | 457 | public RowComparer() {
|
---|
| 458 | }
|
---|
[3312] | 459 |
|
---|
[3346] | 460 | private List<KeyValuePair<int, SortOrder>> sortedIndizes;
|
---|
| 461 | public IEnumerable<KeyValuePair<int, SortOrder>> SortedIndizes {
|
---|
| 462 | get { return this.sortedIndizes; }
|
---|
| 463 | set { sortedIndizes = new List<KeyValuePair<int, SortOrder>>(value); }
|
---|
| 464 | }
|
---|
| 465 | private IStringConvertibleMatrix matrix;
|
---|
| 466 | public IStringConvertibleMatrix Matrix {
|
---|
| 467 | get { return this.matrix; }
|
---|
| 468 | set { this.matrix = value; }
|
---|
| 469 | }
|
---|
| 470 |
|
---|
[3314] | 471 | public int Compare(int x, int y) {
|
---|
| 472 | int result = 0;
|
---|
| 473 | double double1, double2;
|
---|
| 474 | DateTime dateTime1, dateTime2;
|
---|
[3444] | 475 | TimeSpan timeSpan1, timeSpan2;
|
---|
[3314] | 476 | string string1, string2;
|
---|
| 477 |
|
---|
[3346] | 478 | if (matrix == null)
|
---|
| 479 | throw new InvalidOperationException("Could not sort IStringConvertibleMatrix if the matrix member is null.");
|
---|
| 480 | if (sortedIndizes == null)
|
---|
| 481 | return 0;
|
---|
| 482 |
|
---|
| 483 | foreach (KeyValuePair<int, SortOrder> pair in sortedIndizes.Where(p => p.Value != SortOrder.None)) {
|
---|
[3350] | 484 | string1 = matrix.GetValue(x, pair.Key);
|
---|
| 485 | string2 = matrix.GetValue(y, pair.Key);
|
---|
| 486 | if (double.TryParse(string1, out double1) && double.TryParse(string2, out double2))
|
---|
| 487 | result = double1.CompareTo(double2);
|
---|
| 488 | else if (DateTime.TryParse(string1, out dateTime1) && DateTime.TryParse(string2, out dateTime2))
|
---|
| 489 | result = dateTime1.CompareTo(dateTime2);
|
---|
[3444] | 490 | else if (TimeSpan.TryParse(string1, out timeSpan1) && TimeSpan.TryParse(string2, out timeSpan2))
|
---|
| 491 | result = timeSpan1.CompareTo(timeSpan2);
|
---|
[3350] | 492 | else {
|
---|
| 493 | if (string1 != null)
|
---|
| 494 | result = string1.CompareTo(string2);
|
---|
| 495 | else if (string2 != null)
|
---|
| 496 | result = string2.CompareTo(string1) * -1;
|
---|
| 497 | }
|
---|
| 498 | if (pair.Value == SortOrder.Descending)
|
---|
| 499 | result *= -1;
|
---|
| 500 | if (result != 0)
|
---|
| 501 | return result;
|
---|
[3314] | 502 | }
|
---|
| 503 | return result;
|
---|
| 504 | }
|
---|
| 505 | }
|
---|
[3316] | 506 |
|
---|
[4213] | 507 | private void dataGridView_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e) {
|
---|
| 508 | if (Content == null) return;
|
---|
| 509 | if (e.Button == MouseButtons.Right && Content.ColumnNames.Count() != 0)
|
---|
| 510 | contextMenu.Show(MousePosition);
|
---|
| 511 | }
|
---|
[3316] | 512 | private void ShowHideColumns_Click(object sender, EventArgs e) {
|
---|
| 513 | new ColumnsVisibilityDialog(this.dataGridView.Columns.Cast<DataGridViewColumn>()).ShowDialog();
|
---|
| 514 | }
|
---|
[4652] | 515 |
|
---|
| 516 | private void UpdateVisibilityOfTextBoxes() {
|
---|
| 517 | rowsTextBox.Visible = columnsTextBox.Visible = showRowsAndColumnsTextBox;
|
---|
| 518 | rowsLabel.Visible = columnsLabel.Visible = showRowsAndColumnsTextBox;
|
---|
[4709] | 519 | UpdateDataGridViewSizeAndLocation();
|
---|
| 520 | }
|
---|
[4652] | 521 |
|
---|
[4709] | 522 | private void UpdateVisibilityOfStatisticalInformation() {
|
---|
[4779] | 523 | statisticsTextBox.Visible = showStatisticalInformation;
|
---|
[4709] | 524 | UpdateDataGridViewSizeAndLocation();
|
---|
| 525 | }
|
---|
| 526 |
|
---|
| 527 | private void UpdateDataGridViewSizeAndLocation() {
|
---|
[4652] | 528 | int headerSize = columnsTextBox.Location.Y + columnsTextBox.Size.Height +
|
---|
[4709] | 529 | columnsTextBox.Margin.Bottom + dataGridView.Margin.Top;
|
---|
[4652] | 530 |
|
---|
| 531 | int offset = showRowsAndColumnsTextBox ? headerSize : 0;
|
---|
| 532 | dataGridView.Location = new Point(0, offset);
|
---|
[4709] | 533 |
|
---|
[4779] | 534 | int statisticsTextBoxHeight = showStatisticalInformation ? statisticsTextBox.Height + statisticsTextBox.Margin.Top + statisticsTextBox.Margin.Bottom : 0;
|
---|
| 535 | dataGridView.Size = new Size(Size.Width, Size.Height - offset - statisticsTextBoxHeight);
|
---|
[4652] | 536 | }
|
---|
[4709] | 537 |
|
---|
| 538 | private void dataGridView_SelectionChanged(object sender, EventArgs e) {
|
---|
[4779] | 539 | string stringFormat = "{0,20:0.0000}";
|
---|
| 540 | statisticsTextBox.Text = string.Empty;
|
---|
[4709] | 541 | if (dataGridView.SelectedCells.Count > 1) {
|
---|
| 542 | List<double> selectedValues = new List<double>();
|
---|
| 543 | foreach (DataGridViewCell cell in dataGridView.SelectedCells) {
|
---|
| 544 | double value;
|
---|
| 545 | if (!double.TryParse(cell.Value.ToString(), out value)) return;
|
---|
| 546 | selectedValues.Add(value);
|
---|
| 547 | }
|
---|
| 548 | if (selectedValues.Count > 1) {
|
---|
| 549 | StringBuilder labelText = new StringBuilder();
|
---|
[4779] | 550 | labelText.Append("Count: " + string.Format(stringFormat, selectedValues.Count) + " ");
|
---|
| 551 | labelText.Append("Sum: " + string.Format(stringFormat, selectedValues.Sum()) + " ");
|
---|
| 552 | labelText.Append("Min: " + string.Format(stringFormat, selectedValues.Min()) + " ");
|
---|
| 553 | labelText.Append("Max: " + string.Format(stringFormat, selectedValues.Max()) + " ");
|
---|
| 554 | labelText.Append("Average: " + string.Format(stringFormat, selectedValues.Average()) + " ");
|
---|
| 555 | labelText.Append("Standard Deviation: " + string.Format(stringFormat, selectedValues.StandardDeviation()) + " ");
|
---|
[4720] | 556 |
|
---|
[4779] | 557 | statisticsTextBox.Text = labelText.ToString();
|
---|
[4709] | 558 | }
|
---|
| 559 | }
|
---|
| 560 | }
|
---|
[2669] | 561 | }
|
---|
| 562 | }
|
---|