using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using MathNet.Numerics.LinearAlgebra; namespace MatrixCalculator { static class GridViewHelper { /// /// Loads a matrix in a DataGridView /// public static void PutMatrix(Matrix m, DataGridView grid) { // Reset grid grid.Rows.Clear(); grid.Columns.Clear(); // Add columns for (int i = 0; i < m.ColumnCount; i++) { var col = new DataGridViewTextBoxColumn() { SortMode = DataGridViewColumnSortMode.NotSortable, AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells }; grid.Columns.Add(col); } // Add rows and data for (int i = 0; i < m.RowCount; i++) { grid.Rows.Add(); for (int j = 0; j < m.ColumnCount; j++) grid[j, i].Value = MyDouble.String(m[i, j]); } } /// /// Gets the matrix from a DataGridView /// public static Matrix GetMatrix(DataGridView grid) { Matrix m = Matrix.Build.Dense(grid.Rows.Count, grid.Columns.Count); for (int i = 0; i < m.RowCount; i++) for (int j = 0; j < m.ColumnCount; j++) { double temp = 0; if (grid[j, i].Value != null) double.TryParse(grid[j, i].Value.ToString(), out temp); m[i, j] = temp; } return m; } /// /// Resizes a DataGridView /// /// The DataGridView /// The new number of columns /// The new number of rows public static void Resize(DataGridView grid, int ncols, int nrows) { // Add columns while (grid.Columns.Count < ncols) { DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn() { AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells, SortMode = DataGridViewColumnSortMode.NotSortable }; grid.Columns.Add(col); } // Remove columns while (grid.Columns.Count > ncols) grid.Columns.Remove(grid.Columns.GetLastColumn(DataGridViewElementStates.Displayed, DataGridViewElementStates.None)); // Add rows while (grid.Rows.Count < nrows) grid.Rows.Add(); // Remove rows while (grid.Rows.Count > nrows) grid.Rows.RemoveAt(grid.Rows.GetLastRow(DataGridViewElementStates.Displayed)); } /// /// Copies the selected area to clipboard /// public static void CopySelected(DataGridView grid) { int minRow, minCol, maxRow, maxCol; minRow = minCol = int.MaxValue; maxRow = maxCol = int.MinValue; foreach (DataGridViewCell i in grid.SelectedCells) { minRow = Math.Min(minRow, i.RowIndex); minCol = Math.Min(minCol, i.ColumnIndex); maxRow = Math.Max(maxRow, i.RowIndex); maxCol = Math.Max(maxCol, i.ColumnIndex); } string csv = CsvParser.ToCSV(grid, minRow, minCol, maxRow, maxCol, true); Clipboard.SetText(csv, TextDataFormat.CommaSeparatedValue); } /// /// Pastes clipboard content into selected cell and beyond /// public static void Paste(DataGridView grid) { // Where to paste int row = 0, col = 0; if (grid.CurrentCell != null) { row = grid.CurrentCell.RowIndex; col = grid.CurrentCell.ColumnIndex; } // Get data string data = ""; if (Clipboard.ContainsText(TextDataFormat.CommaSeparatedValue)) data = Clipboard.GetText(TextDataFormat.CommaSeparatedValue); else if (Clipboard.ContainsText()) data = Clipboard.GetText(); var table = CsvParser.GetTable(data, MatrixCalculator.Properties.Settings.Default.InternalCsvSeparators.ToArray()); // Put data in table for (int i = 0; i < table.Rows.Count; i++) for (int j = 0; j < table.Columns.Count; j++) if (table.Rows[i][j].ToString() != "" && row + i < grid.Rows.Count && col + j < grid.Columns.Count) grid[j + col, i + row].Value = table.Rows[i][j]; } } }