2010-11-06 8 views

Répondre

17

Suivez l'exemple de code sur cette page pour remplir la propriété Rows:

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

Modifier

Transforme c'est un peu plus épineux que je pensais. Voici un exemple de code:

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

pour obtenir la solution de Merlyn fonctionne, vous devrez définir la colonne compte avant d'ajouter des lignes à la datagridview:

dataGridView1.ColumnCount = 3; 
+2

Il devrait être un commentaire pas une solution – boctulus

+1

Cela devrait. J'étais nouveau à stackoverflow au moment où j'ai écrit la réponse (commentaire). – randoms

Questions connexes