簡體   English   中英

如何將DataGrid上特定行的值作為參數傳遞?

[英]How do I pass values from a specific row on a DataGrid as a parameter?

我有一個DataGridView,我希望將特定行上的所有值都傳遞給新表單。 我知道如何提取值,但是如何將這些值傳遞到新表單中(每個值都將分配給特定的文本框)?

您是否要從新表單中隱藏其余的DataGridView? 例如,如果將DataGridViewRow傳遞給表單,則新表單可以直接獲取值。 但這也將使他們能夠訪問DataGridView。

DataGridViewRow dgr = dataGridView.Rows[0];
MyForm myForm = new MyForm(dgr);  // now the form has the Row of information. But it can also access the entire DataGridView as it is available in the DataGridViewRow.

如果要刪除對DataGridView的訪問權限,則將行中的值放入容器中並傳遞該容器。 您可以使用內置的東西,例如ArrayList,List等。或創建自己的自定義容器。

我將創建一個類,該類具有數據集中每個列的屬性。 然后,您可以使用班級列表來存儲所有內容,並且列表中的每個元素都是一行。 然后,您可以設置DataGridView.DataSource = yourList 當您需要傳遞一列時,只需傳遞列表中的那個元素即可。

如果確實不需要為數據創建類,則還可以將DataGridViewRow傳遞給函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM