2009-07-22 16 views
0

現在のプログラムが動的に作成されたDataGridViewから情報を取得しようとしています。私は、グリッドに情報を取得し、必要な検索を実行することができましたが、今は本当に立ち往生しています。DataGridViewから動的にデータを取得するC#

各行内にボタンを保持する列をdatagridviewに追加しました。私がしたいのは、クリックされたボタンと同じ行にある列インデックス1のデータの値を取ることです。混乱している?とにかく、コードは次のとおりです:

 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??? 
     } 

ありがとうございました!

David。

答えて

0

DataGridViewCellEventArgsには、RowIndexなどの非常に有用な情報が含まれています。

ので(私はあなたが値で何をしたいのか分からない)のようなもの:

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

おかげで非常に多く、非常に便利! –

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

非常に深い - "Bool val"変数は何のためにあるのだろうか? –

関連する問題