c# - Show 2d-array in DataGridView -
i have 2d array. want print array in datagridview
throws error:
[argument outofrangeexception unhandled ]
this code
for (int j = 0; j < height; j++) { (int = 0; < width; i++) { datagridview1[i, j].value = state[i, j].h; //state[i, j].h array datagridview1[i, j].style.backcolor pixelcolor[i,j]; datagridview1[i, j].style.forecolor = color.gold; } }
as comments have pointed out, should focus on rows , cells. need build datagridview
columns , populate each row cell cell.
the width
of array should correspond dgv columns , height
dgv rows. take following 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; (int r = 0; r < height; r++) { datagridviewrow row = new datagridviewrow(); row.createcells(this.datagridview1); (int c = 0; c < width; c++) { row.cells[c].value = twod[r, c]; } this.datagridview1.rows.add(row); }
Comments
Post a Comment