I have a 2D array. I want to print the array in my DataGridView
but it throws an error:
[Argument OutOfRangeException was unhandled ] >
As comments have pointed out, you should focus on rows and cells. You need to build your DataGridView
columns and then populate each row cell by cell.
The width
of your array should correspond to your dgv columns and the height
to the dgv rows. Take the following as a simple example:
string[,] twoD = new string[,]
{
{"row 0 col 0", "row 0 col 1", "row 0 col 2"},
{"row 1 col 0", "row 1 col 1", "row 1 col 2"},
{"row 2 col 0", "row 2 col 1", "row 2 col 2"},
{"row 3 col 0", "row 3 col 1", "row 3 col 2"},
};
int height = twoD.GetLength(0);
int width = twoD.GetLength(1);
this.dataGridView1.ColumnCount = width;
for (int r = 0; r < height; r++)
{
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(this.dataGridView1);
for (int c = 0; c < width; c++)
{
row.Cells[c].Value = twoD[r, c];
}
this.dataGridView1.Rows.Add(row);
}