2011-08-11 142 views

回答

2

首先爲Gapton說,你需要手工CellDoubleClick事件,這個事件裏面,你可以得到使用以下語法當前行的單元格的值:

object cell1 = dataGrid.Rows[e.RowIndex].Cells[0].Value; 
object cell2 = dataGrid.Rows[e.RowIndex].Cells[2].Value; 

其中e.RowIndex是行用戶雙擊索引點擊和e.ColumnIndex包含此雙擊出現的單元格的列索引...

現在,要將值傳遞給新窗體,可以通過兩種不同方式執行此操作: 1:使用公共屬性,假設您有Form2你想要傳遞值,在Form2類中定義prop爲你的感興趣的值ERTIES如:

public object cell1 { get; set; } 
public object cell2 { get; set; } 

並在CellDoubleClick以上,實例窗體2的新對象,將值分配給屬性並調用顯示方法來顯示這種形式:

private void dataGrid_CellDoubleClick(object sender, DataGridViewCellEventArgs e) 
     { 
      object cell1 = dataGrid.Rows[e.RowIndex].Cells[0].Value; 
      object cell2 = dataGrid.Rows[e.RowIndex].Cells[2].Value; 

Form2 form2 = new Form2(); 
form2.cell1 = cell1; 
... 
form2.Show(); 
     } 

2:使用重載的構造,寫窗體2的重載的構造,這樣的:

public Form2(object cell1, ...) { 
this.cell1 = cell1; 
.... 
InitializeComponent(); 
} 

,然後在事件處理程序:

private void dataGrid_CellDoubleClick(object sender, DataGridViewCellEventArgs e) 
     { 
      object cell1 = dataGrid.Rows[e.RowIndex].Cells[0].Value; 
      object cell2 = dataGrid.Rows[e.RowIndex].Cells[2].Value; 

Form2 form2 = new Form2(cell1...); 
form2.Show(); 
     } 
+0

非常感謝Waqas的非常詳細,工作的解釋! :) –

1

在「事件」面板中,您可以指定一個函數在雙擊某行時調用。在函數中,您可以使用DataGridViewRow.Cells [index] .Value來訪問單元格的值,然後將其傳遞給一個新窗體用於任何目的。

或者,您可以傳遞整個DataGridViewRow: dataGridView1.CurrentRow將爲您提供當前選定的DataGridViewRow。

+0

非常感謝.. :) –