在显示2D的DataGridView阵列 [英] Show 2d-array in DataGridView
本文介绍了在显示2D的DataGridView阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个二维数组。我想打印数组中的我的 DataGridView的
,但它抛出一个错误:
[参数OutOfRangeException了未处理]
块引用>这是我的code
为(INT J = 0; J<高度; J ++)
{
的for(int i = 0; I<宽度;我++)
{
dataGridView1 [I,J]。价值=状态[I,J] .H;
//状态[I,J] .H这是我的数组
dataGridView1 [I,J] .Style.BackColor pixelcolor [I,J]。
dataGridView1 [I,J] .Style.ForeColor = Color.Gold;
}
}
解决方案由于意见所指出的那样,你应该专注于行和单元格。你需要建立你的
DataGridView的
列,然后通过细胞填充每一行的单元格。您阵的
宽度应符合您的DGV列和
高度
来的DGV行。采取以下作为一个简单的例子:字符串[,]数组twoD =新的字符串[,]
{
{排0 COL 0,0行山口1,0行栏2},
{行1列0,(1,1),行1列2},
{行2列0,行2列1,行2列2},
{排3 COL 0,3排第1栏,第3行第2栏},
};INT高度= twoD.GetLength(0);
INT宽度= twoD.GetLength(1);this.dataGridView1.ColumnCount =宽度;对于(INT R = 0;为r的高度; R ++)
{
一个DataGridViewRow行=新的DataGridViewRow();
row.CreateCells(this.dataGridView1); 对于(INT C = 0; C<宽度; C ++)
{
row.Cells [C]。价值=数组twoD [R,C]。
} this.dataGridView1.Rows.Add(行);
}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: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); }
这篇关于在显示2D的DataGridView阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文