145 lines
4.9 KiB
C#
145 lines
4.9 KiB
C#
|
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
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Loads a matrix in a DataGridView
|
|||
|
/// </summary>
|
|||
|
public static void PutMatrix(Matrix<double> 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]);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Gets the matrix from a DataGridView
|
|||
|
/// </summary>
|
|||
|
public static Matrix<double> GetMatrix(DataGridView grid)
|
|||
|
{
|
|||
|
Matrix<double> m = Matrix<double>.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;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Resizes a DataGridView
|
|||
|
/// </summary>
|
|||
|
/// <param name="grid">The DataGridView</param>
|
|||
|
/// <param name="ncols">The new number of columns</param>
|
|||
|
/// <param name="nrows">The new number of rows</param>
|
|||
|
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));
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Copies the selected area to clipboard
|
|||
|
/// </summary>
|
|||
|
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);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Pastes clipboard content into selected cell and beyond
|
|||
|
/// </summary>
|
|||
|
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];
|
|||
|
}
|
|||
|
}
|
|||
|
}
|