0

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;
    }
}
AliciaBytes
  • 7,300
  • 6
  • 36
  • 47
lena lena
  • 49
  • 1
  • 1
  • 6
  • Your code is looking for an array of DataGridViews. You probably want to target the rows and cells. – LarsTech Apr 14 '15 at 16:52
  • You should add rows, like in this post http://stackoverflow.com/questions/13362971/adding-rows-on-datagridview-manually – Ilia Maskov Apr 14 '15 at 17:01
  • I use for (int j = 0; j < height; j++) for (int i = 0; i < width; i++) { dataGridView1.Rows.Add(new object[] {true,state[i,j].h}); } but I have this error InvalidOperationException was unhandle – lena lena Apr 14 '15 at 17:16

3 Answers3

4

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);
}
OhBeWise
  • 5,350
  • 3
  • 32
  • 60
  • I have question please how can I make each cell be smaller – lena lena Apr 14 '15 at 17:55
  • @lenalena A good way is to play with the value of `AutoSizeColumnsMode`. For example, if you want all columns to fit the largest displayed text per column, do this: `this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;` – OhBeWise Apr 14 '15 at 17:57
  • @lenalena Similarly, you could also change such for individual columns: `dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;` Conversely, you could just manually set the width: `dataGridView1.Columns[1].Width = 50;` – OhBeWise Apr 14 '15 at 18:17
  • and the height how can resize it like this or out dataGridView1.Rows[1].Height = 80; – lena lena Apr 14 '15 at 18:30
1

for exemple for 2 elements

dataGridView1.ColumnCount = 2;
var dataArray = new int[] { 3, 4, 4, 5, 6, 7, 8 };
for (int i = 0; i < dataArray.Count; i++)
{
   dataGridView1.Rows.Add(new object[] { i, dataArray[i] });
}
0

The first potential problem is with how you are accessing your array indexes. Which can be handled this way.

string[,] a = {
  {"0", "1", "2"},
      {"0", "1", "2"},
      {"0", "1", "2"},
      {"0", "1", "2"},
  };

for (int i = 0; i < a.GetLength(0); i++)
{
    for (int j = 0; j < a.GetLength(1); j++)
    {
        Console.WriteLine(a[i,j]);
    }
}

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.

Andy Hoffner
  • 3,267
  • 2
  • 21
  • 22
IdahoSixString
  • 643
  • 10
  • 18