2012-10-12 59 views
1

我不確定這是否可能,但我想獲取DataGridView單元格內的按鈕的實例。 (使用c#.net和windows窗體)如何獲取DataGridView單元格內的按鈕的實例?

原因是因爲我想彈出一個控制面板靠近那個按鈕,所以我需要它的位置。 (這可能嗎?)。

我處理按鈕的點擊在CellClicked事件處理程序:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) 
    { 
     Int32 id = (Int32)dataGridView1[0, e.RowIndex].Value; 
     if (e.RowIndex < 0 || e.ColumnIndex != dataGridView1.Columns["NewDate"].Index) 
     { 
      if (e.RowIndex < 0 || e.ColumnIndex != dataGridView1.Columns["Delete"].Index) return; 
      else 
      { 

      } 
     } 
     else 
     { 
      //Button button - the button I'm trying to get 
      panel2.Location=new Point(button.Location.X,button.Location.Y); 

     } 

    } 
+0

你檢查了答案嗎? –

回答

1

你沒有拿到按鈕,但點擊的單元格的矩形。檢查代碼:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) 
    { 
     Int32 id = (Int32)dataGridView1[0, e.RowIndex].Value; 
     if (e.RowIndex < 0 || e.ColumnIndex != dataGridView1.Columns["NewDate"].Index) 
     { 
      if (e.RowIndex < 0 || e.ColumnIndex != dataGridView1.Columns["Delete"].Index) return; 
      else 
      { 

      } 
     } 
     else 
     { 
      Rectangle rect = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false); 
      panel2.Location=new Point(rect.X,rect.Y); 

     } 

    } 
+0

我無法試用atm –

相關問題