我一直在尝试在 C# Winform DataTable 中构建一个简单的网格计算,其中用户输入起始值、结束值、展开起始值和展开结束值(在下面的代码中,我只是硬编码了这些值)。从那里,该表构建了一个计算值矩阵。
例如(初始结构):
价格 ABCD
1
2
3
4
计算值:
1 - A、1 - B、1 - C、1 - D、2 - A、2 - B、2 - C、2 - D、3 - A、3 - B、3 - C、3 - D、4 - A, 4 - B, 4 - C, 4 - D,
下面的网格是我第一次尝试构建计算网格 - 有什么比下面更好的解决方案?:
static DataTable GetCalcGrid()
{
DataTable table = new DataTable();
table.Clear();
DataColumn column;
DataRow row;
try
{
//double minValue = (double)startingPrice.Value;
double minValue = 1;
//double maxValue = (double)endingPrice.Value;
double maxValue = 2;
//double incrementValue = (double)incrementPrice.Value;
double incrementValue = .25;
//double minSpreadValue = (double)spreadRangeLow.Value;
double minSpreadValue = -.50;
//double maxSpreadValue = (double)spreadRangeHigh.Value;
double maxSpreadValue = .50;
//double incrementSpreadValue = (double)spreadIncrement.Value;
double incrementSpreadValue = .25;
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "N*";
column.AutoIncrement = true;
column.AutoIncrementSeed = 1;
column.AutoIncrementStep = 1;
table.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType("System.Double");
column.ColumnName = "Price";
table.Columns.Add(column);
for (double x = minSpreadValue; x < maxSpreadValue + incrementSpreadValue; x += incrementSpreadValue)
{
double value;
value = x;
column = new DataColumn();
column.DataType = System.Type.GetType("System.Double");
column.ColumnName = value.ToString("0.000");
table.Columns.Add(column);
table.Columns[value.ToString("0.000")].Expression = "(([Price] + (" + value + ")))";
//table.Columns[value.ToString("0.000")].Expression = " " + OP.black("C",4,Convert.ToDouble("(([Price] + (" + value + ")))"),.32,15,365,.003,1).ToString() + " ";
}
for (double i = minValue; i < maxValue + incrementValue; i += incrementValue)
{
row = table.NewRow();
row["Price"] = i;
table.Rows.Add(row);
}
}
catch (Exception ex) { MessageBox.Show("! " + ex); }
finally { }
return table;
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = GetCalcGrid();
}