2009-07-22 8 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??? 
     } 

도움을 주셔서 감사합니다.

데이비드.

답변

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"변수가 무엇인지 궁금합니다. –

관련 문제