2

Can somebody help with this snippet of code which has an exception. It always stacks on [2,0] index. I just can't figure out how to solve this. I have added a screenshot below.

private Matrix GridToMatrix(DataGridView grid)
{
    var matrix = new double[grid.RowCount, grid.ColumnCount];

    for (int i = 0; i < grid.RowCount; i++)
    {
        for (int j = 0; j < grid.ColumnCount; j++)
        {
            matrix[i, j] = Convert.ToSingle(grid[i, j].Value);
        }
    }

    return new Matrix(matrix);
}

enter image description here

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
eek
  • 93
  • 1
  • 1
  • 6

1 Answers1

0

The problem here is the confusing indexing mechanism of DataGridView.

The indexing for DataGridView is like [col, row] while Matrix indexing is [row, col]

By changing grid[i, j] to grid[j, i] the problem will resolve.

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74