Show 2d-array in DataGridView

后端 未结 3 1636
不知归路
不知归路 2021-01-16 19:13

I have a 2D array. I want to print the array in my DataGridView but it throws an error:

[Argument OutOfRangeException was unhandled ]

3条回答
  •  被撕碎了的回忆
    2021-01-16 19:19

    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.

提交回复
热议问题