2009-07-22 9 views
0

J'essaie d'obtenir mon programme actuel pour prendre des informations à partir d'un DataGridView créé dynamiquement. J'ai réussi à obtenir l'information dans la grille, et effectuer la recherche requise, mais maintenant je suis vraiment coincé.C# prendre dynamiquement des données à partir de DataGridView

J'ai ajouté une colonne à datagridview qui contient un bouton dans chaque ligne. Ce que je voudrais faire est de prendre la valeur des données de l'index de colonne 1 qui est dans la même rangée que le bouton cliqué. Déroutant? Quoi qu'il en soit, voici le code:

 public void GetValues(...) 
    { 


     //Details regarding connection, querying and inserting table 
     . 
     . 
     . 

     DataGridViewButtonColumn buttonCol = new DataGridViewButtonColumn(); 
     buttonCol.Name = "ButtonColumn"; 
     buttonCol.HeaderText = "Select"; 
     buttonCol.Text = "Edit"; 
     //NB: the text won't show up on the button. Any help there either? 

     dataGridView1.Columns.Add(buttonCol); 
     dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick); 
     foreach (DataGridViewRow row in dataGridView1.Rows) 
     { 
      DataGridViewButtonCell button = (row.Cells["ButtonColumn"] as DataGridViewButtonCell); 

     } 
     dataGridView1.Columns["ButtonColumn"].DisplayIndex = 0; 


    } 


     void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) 
     { 
      //Here is where I'm having the trouble. What do I put in here??? 
     } 

Merci pour toute aide que vous pouvez donner!

David.

Répondre

0

Votre DataGridViewCellEventArgs contient des informations très utiles telles que RowIndex.

donc quelque chose comme (je ne sais pas ce que vous voulez faire avec la valeur):

String dataYouWant = dataGridView1.Rows[e.RowIndex].Cells[1].Value; 
+0

Merci beaucoup, très utile! –

0

`

if (e.ColumnIndex != button_column_number) //column number of the button. 
        return; 
       dataGridView1.EndEdit(); 
       bool val; 
       if ((dataGridView1.Rows[e.RowIndex].Cells[1].Value) != null) // column index 1...as that's what you want. 
       { 
        //d stuff you want here. 
       } 
       else 
       { 
       } 

`

+0

Très approfondi - je me demandais à quoi sert la variable "Bool val"? –

Questions connexes