2010-11-06 86 views

回答

17

按照代码示例此页面上填充Rows属性:

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.aspx

编辑

原来,这比我想象的有点棘手。下面是一个代码示例:

var data = new int[4,3] 
{ 
    { 1, 2, 3, }, 
    { 4, 5, 6, }, 
    { 7, 8, 9, }, 
    { 10, 11, 12 }, 
}; 

var rowCount = data.GetLength(0); 
var rowLength = data.GetLength(1); 

for (int rowIndex = 0; rowIndex < rowCount; ++rowIndex) 
{ 
    var row = new DataGridViewRow(); 

    for(int columnIndex = 0; columnIndex < rowLength; ++columnIndex) 
    { 
     row.Cells.Add(new DataGridViewTextBoxCell() 
      { 
       Value = data[rowIndex, columnIndex] 
      }); 
    } 

    dataGridView1.Rows.Add(row); 
} 
11

得到Merlyn的解决方案正常工作,您需要设置列算你行添加到DataGridView前:

dataGridView1.ColumnCount = 3; 
+2

这应该是一个评论而不是解决方案 – boctulus 2016-03-01 00:14:43

+1

这应该。当我写答案(评论)的时候,我是新的stackoverflow。 – randoms 2016-03-01 06:40:44