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

时间:2010-04-09 20:22:00

标签: c# winforms datagridview

我有一个DataGridView,我希望特定行上的所有值都传递给新表单。我知道如何提取值,但是我应该如何将这些值传递给新表单(每个都将分配给特定的文本框)?

2 个答案:

答案 0 :(得分:4)

是否要从新表单中隐藏其余的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等内置的东西。或者创建自己的自定义容器。

答案 1 :(得分:1)

我会创建一个具有数据集中每列的属性的类。然后,您可以使用类的列表来存储所有内容,列表的每个元素都是一行。然后,您可以设置DataGridView.DataSource = yourList。当你需要传递一个列时,只需要遍历列表中的那个元素。

如果您确实不需要为数据创建课程,也可以将DataGridViewRow传递给您的函数。

相关问题