我一直在尝试在C#Winform DataTable中构建一个简单的网格计算,其中用户输入起始值,结束值,扩展起始值和扩展结束值(在下面的代码中我只是硬编码了值)。从那里,该表构建计算值的矩阵。
例如(初始结构):
价格A B C D
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();
}
答案 0 :(得分:1)
您可以尝试这样的事情:
public class Matrix : List<MatrixEntry>
{
}
public class MatrixEntry
{
public double Price { get; private set; }
public double Value1
{
get { return (Price - 0.5); }
}
public double Value2
{
get { return (Price - 0.25); }
}
public double Value3
{
get { return Price; }
}
public double Value4
{
get { return (Price + 0.25); }
}
public double Value5
{
get { return (Price + 0.5); }
}
public MatrixEntry(double price)
{
Price = price;
}
}
static Matrix GetMatrixCalcGrid()
{
var matrix = new Matrix();
try
{
double minValue = 1;
double maxValue = 2;
double incrementValue = .25;
for (double i = minValue; i < maxValue + incrementValue; i += incrementValue)
{
var entry = new MatrixEntry(i);
matrix.Add(entry);
}
}
catch (Exception ex) { Console.WriteLine("! " + ex); }
finally { }
return matrix;
}
这是你要找的吗?
注意:我显然已对计算进行了硬编码..但您可以添加一些额外的属性来存储用户输入并修改属性以返回正确的值。我的例子的主要观点是我认为它比使用DataTable更有效。