I have a 2D array. I want to print the array in my DataGridView
but it throws an error:
[Argument OutOfRangeException was unhandled ]
This is my code
for (int j = 0; j < height; j++)
{
for (int i = 0; i < width; i++)
{
dataGridView1[i, j].Value = state[i, j].h;
//state[i, j].h this is my array
dataGridView1[i, j].Style.BackColor pixelcolor[i,j];
dataGridView1[i, j].Style.ForeColor = Color.Gold;
}
}
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 theheight
to the dgv rows. Take the following as a simple example:for exemple for 2 elements
The first potential problem is with how you are accessing your array indexes. Which can be handled this way.
Just check your array dimension length first. Clearly one of your variables height or width is incorrect.
This is done using
Array.GetLength(int dimension)
The second problem is how you are adding items to your datagridview.