0

我一直在尝试在 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();
    }
4

1 回答 1

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 更有效。

于 2013-03-10T02:58:05.640 回答