我需要從雙擊行的數據網格中獲取數據到一個新的表單,我剛剛深入到dotnet開發,請指導的方式來做。DataGridViewRow到表格
0
A
回答
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();
}
1
在「事件」面板中,您可以指定一個函數在雙擊某行時調用。在函數中,您可以使用DataGridViewRow.Cells [index] .Value來訪問單元格的值,然後將其傳遞給一個新窗體用於任何目的。
或者,您可以傳遞整個DataGridViewRow: dataGridView1.CurrentRow將爲您提供當前選定的DataGridViewRow。
+0
非常感謝.. :) –
相關問題
- 1. 構建DataGridViewRow並添加到數據表
- 2. 根據單元格內容刪除DataGridViewRow
- 3. 如何添加ComboBox到C#中的DataGridViewRow?
- 4. C#:將DataGridViewRow添加到DataGridView異常
- 5. 按值複製DataGridViewRow?
- 6. 如何跳過DataGridViewRow?
- 7. DataGridViewRow選擇操作
- 8. 更改DataGridViewRow的BackColor
- 9. DataGridViewRow數值順序
- 10. 自定義DataGridViewRow問題
- 11. 在C中擴展DataGridViewRow#
- 12. c#winforms - 如何使DataGridViewRow UnSelectable?
- 13. 如何從DataGridViewRow填充DataGridView
- 14. 如何取消共享Datagridviewrow
- 15. Datagridviewrow返回單元格的不正確的值
- 16. 添加的DataGridViewRow手動使空單元格
- 17. 如何使用帶有DataGridViewRow列表的BindingSource DataSource?
- 18. 如何確定哪個DataRow綁定到DataGridViewRow
- 19. 如何從CellFormatting事件中獲取DataGridViewRow?
- 20. 在選定的索引處選擇DataGridViewRow
- 21. 在1小時內將DataGridViewRow移動1cm
- 22. 的DataGridViewRow不被垃圾收集
- 23. 如何記住DataGridViewRow隱藏屬性
- 24. 在特定行開始'For Each As DataGridViewRow'
- 25. Excel表格到Html表格
- 26. HTML表格到ExtJS表格
- 27. Django表格到db表格
- 28. 將表格行添加到表格
- 29. 將表格行移動到新表格
- 30. 使表格可以滾動到表格
非常感謝Waqas的非常詳細,工作的解釋! :) –