如何在C#中将字符串作为数据网格视图数据源

时间:2017-07-14 13:47:52

标签: c# arrays datagridview

我有一个包含25个字母的字符串,我希望通过将字符串转换为[5,5]元素的二维char数组,将此字符串作为数据网格视图的数据源。我写的代码如下,我执行的错误是:

  

"指数超出范围。必须是非负数且小于集合的大小。"

char[,] keyMatrix = new char[5, 5];
int counter=0;
this.dataGridView1.ColumnCount = 5;
this.dataGridView1.RowCount = 5;
for(int i=0;i<5;i++)
{
    DataGridViewRow row = new DataGridViewRow();
    row.CreateCells(this.dataGridView1);
    for(int j=0;j<5;j++)
    {
       keyMatrix[i, j] = textBox2.Text[counter];
       row.Cells[j].Value = keyMatrix[i, j];
       counter++;
    }
    this.dataGridView1.Rows.Add(row);
}

0 个答案:

没有答案