开发者

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

开发者 https://www.devze.com 2022-12-26 01:45 出处:网络
I have a DataGridView that I would like all values on a specific row passed to a new form.I know how to extract the开发者_JAVA百科 values, but how should I pass these values into the new form (each wi

I have a DataGridView that I would like all values on a specific row passed to a new form. I know how to extract the开发者_JAVA百科 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? For instance if you pass a DataGridViewRow to the form the new form can get the values directly. But that would also give them access to the 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. You could use something built in like an ArrayList, List, etc.. or create your own custom container.


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. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消