简体   繁体   English

如何将DataGrid上特定行的值作为参数传递?

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

I have a DataGridView that I would like all values on a specific row passed to a new form. 我有一个DataGridView,我希望将特定行上的所有值都传递给新表单。 I know how to extract the values, but how should I pass these values into the new form (each will be assigned to a specific textbox)? 我知道如何提取值,但是如何将这些值传递到新表单中(每个值都将分配给特定的文本框)?

Do you want to hide the rest of the DataGridView from the new form? 您是否要从新表单中隐藏其余的DataGridView? For instance if you pass a DataGridViewRow to the form the new form can get the values directly. 例如,如果将DataGridViewRow传递给表单,则新表单可以直接获取值。 But that would also give them access to the DataGridView. 但这也将使他们能够访问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.

If you want to remove access to the DataGridView then place the values in the row into a container and pass that container. 如果要删除对DataGridView的访问权限,则将行中的值放入容器中并传递该容器。 You could use something built in like an ArrayList, List, etc.. or create your own custom container. 您可以使用内置的东西,例如ArrayList,List等。或创建自己的自定义容器。

I would create a class that has a property for each column in your dataset. 我将创建一个类,该类具有数据集中每个列的属性。 Then you can use a list of your class to store everything, and each element of the list would be a row. 然后,您可以使用班级列表来存储所有内容,并且列表中的每个元素都是一行。 You can then set your DataGridView.DataSource = yourList . 然后,您可以设置DataGridView.DataSource = yourList When you need to pass a column along, just pass through that element of the list. 当您需要传递一列时,只需传递列表中的那个元素即可。

If you really don't need to make a class for your data, you could also just pass a DataGridViewRow to your function. 如果确实不需要为数据创建类,则还可以将DataGridViewRow传递给函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM