207 lines
6.5 KiB
C#
207 lines
6.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Volian.Controls.Library
|
|
{
|
|
public partial class TablePropertiesControl : UserControl
|
|
{
|
|
private DataTable values;
|
|
private BindingSource bindingSource = null;
|
|
private int totalrows = 1;
|
|
private int totalcols = 1;
|
|
|
|
//initialization / data will be:
|
|
// totalnumrows,totalnumcols;(row,col):value|(row,col):value...
|
|
public TablePropertiesControl(string title, string initialvalues)
|
|
{
|
|
InitializeComponent();
|
|
|
|
lbltitle.Text = title;
|
|
initializevalues(initialvalues);
|
|
bindingSource = new BindingSource();
|
|
bindingSource.DataSource = values;
|
|
dataview.DataSource = bindingSource;
|
|
dataview.AutoGenerateColumns = false;
|
|
|
|
Load += new EventHandler(FormLoad_setDefaults);
|
|
}
|
|
|
|
//initialization / data will be:
|
|
// totalnumrows,totalnumcols;(row,col):value|(row,col):value...
|
|
private void initializevalues(string initialvalues)
|
|
{
|
|
values = new DataTable();
|
|
|
|
if (!string.IsNullOrEmpty(initialvalues))
|
|
{
|
|
string[] grp = initialvalues.Split(';');
|
|
string[] total = grp[0].Split(',');
|
|
initializedt(int.Parse(total[0]), int.Parse(total[1]));
|
|
|
|
string[] ivs = grp[1].Split('|');
|
|
//pair will be of format:
|
|
//(row,col):value
|
|
foreach (string pair in ivs)
|
|
{
|
|
Match m = Regex.Match(pair, @"\(([\d]),([\d])\):([\w])");
|
|
if (m.Success)
|
|
{
|
|
int row = int.Parse(m.Groups[1].Value);
|
|
int col = int.Parse(m.Groups[2].Value);
|
|
string v = m.Groups[3].Value;
|
|
values.Rows[row][col] = v;
|
|
}
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
initializedt(1, 1);
|
|
}
|
|
}
|
|
|
|
//initialize the blank datatable
|
|
private void initializedt(int numrows, int numcols)
|
|
{
|
|
totalrows = numrows;
|
|
totalcols = numcols;
|
|
|
|
BindingSource bindingSourceDropDown = new BindingSource();
|
|
bindingSourceDropDown.DataSource = Enum.GetNames(typeof(EPinputtype));
|
|
|
|
var cName = new DataGridViewComboBoxColumn();
|
|
cName.DataSource = bindingSourceDropDown;
|
|
|
|
//foreach (string n in Enum.GetNames(typeof(EPinputtype))) cName.Items.Add(n);
|
|
cName.DefaultCellStyle.NullValue = "none";
|
|
|
|
for (int c = 0; c < totalcols; c++)
|
|
{
|
|
dataview.Columns.Add(cName);
|
|
values.Columns.Add();
|
|
//dataview.Columns[c].CellTemplate = new DataGridViewComboBoxCell() { DataSource = bindingSourceDropDown };
|
|
}
|
|
|
|
//values.Columns.Add(new DataColumn() { DefaultValue = "none" });
|
|
for (int rw = 0; rw < totalrows; rw++)
|
|
{
|
|
values.Rows.Add();
|
|
}
|
|
|
|
}
|
|
|
|
void FormLoad_setDefaults(object sender, EventArgs e)
|
|
{
|
|
BindingSource bindingSourceDropDown = new BindingSource();
|
|
bindingSourceDropDown.DataSource = Enum.GetNames(typeof(EPinputtype));
|
|
|
|
var cName = new DataGridViewComboBoxColumn();
|
|
cName.DataSource = bindingSourceDropDown;
|
|
cName.DefaultCellStyle.NullValue = "none";
|
|
|
|
for (int c = 0; c < totalcols; c++)
|
|
{
|
|
dataview.Columns.RemoveAt(c);
|
|
dataview.Columns.Insert(c, cName);
|
|
}
|
|
}
|
|
|
|
//Get storage string for storing table values in db
|
|
// data will be:
|
|
// totalnumrows,totalnumcols;(row,col):value|(row,col):value...
|
|
public string GetStorageValue()
|
|
{
|
|
StringBuilder bldr = new StringBuilder();
|
|
bldr.Append($"{totalrows},{totalcols}");
|
|
List<string> points = new List<string>();
|
|
for (int rw = 0; rw < values.Rows.Count; rw++)
|
|
{
|
|
for (int col = 0; col < values.Columns.Count; col++)
|
|
{
|
|
if ((string) values.Rows[rw][col] != "none")
|
|
points.Add($"({rw},{col}):{values.Rows[rw][col]}");
|
|
}
|
|
}
|
|
|
|
|
|
if (points.Count > 0)
|
|
{
|
|
bldr.Append(";");
|
|
bldr.Append(string.Join("|", points));
|
|
}
|
|
|
|
return bldr.ToString();
|
|
}
|
|
|
|
private void NumRows_ValueChanged(object sender, EventArgs e)
|
|
{
|
|
int endNumRows = (int)NumRows.Value;
|
|
int curNumRows = totalrows;
|
|
|
|
//remove rows till equal
|
|
while (curNumRows > endNumRows)
|
|
{
|
|
values.Rows.RemoveAt(curNumRows - 1);
|
|
curNumRows--;
|
|
}
|
|
//add rows till equal
|
|
while (curNumRows < endNumRows)
|
|
{
|
|
values.Rows.Add();
|
|
curNumRows++;
|
|
}
|
|
|
|
totalrows = endNumRows;
|
|
}
|
|
|
|
private void NumCols_ValueChanged(object sender, EventArgs e)
|
|
{
|
|
int endNumCols = (int)NumCols.Value;
|
|
int curNumCols = totalcols;
|
|
|
|
BindingSource bindingSourceDropDown = new BindingSource();
|
|
bindingSourceDropDown.DataSource = Enum.GetNames(typeof(EPinputtype));
|
|
|
|
var cName = new DataGridViewComboBoxColumn();
|
|
cName.DataSource = bindingSourceDropDown;
|
|
cName.DefaultCellStyle.NullValue = "none";
|
|
|
|
//remove cols till equal
|
|
while (curNumCols > endNumCols)
|
|
{
|
|
values.Columns.RemoveAt(curNumCols - 1);
|
|
dataview.Columns.RemoveAt(curNumCols - 1);
|
|
curNumCols--;
|
|
}
|
|
//add cols till equal
|
|
while (curNumCols < endNumCols)
|
|
{
|
|
values.Columns.Add(new DataColumn() { DefaultValue = "none"});
|
|
if (dataview.Columns.Count > curNumCols) dataview.Columns.RemoveAt(curNumCols);
|
|
dataview.Columns.Add(cName);
|
|
|
|
curNumCols++;
|
|
}
|
|
|
|
totalcols = endNumCols;
|
|
|
|
}
|
|
|
|
public enum EPinputtype
|
|
{
|
|
none,
|
|
textbox,
|
|
checkbox
|
|
};
|
|
|
|
}
|
|
|
|
} |