2009-07-22 2 views
0

Я пытаюсь заставить свою текущую программу принимать информацию из динамически созданного DataGridView. Мне удалось получить информацию в сетке и выполнить необходимый поиск, но теперь я действительно застрял.C# динамически беру данные из DataGridView

Я добавил столбец в 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»? –

Смежные вопросы