2010-11-06 83 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