简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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