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

Answers


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.


Need Your Help

How do I return a 401 authentication error to a RestSharp client?

asp.net-mvc restsharp http-error

In my Mvc Api controller, if the user cannot be authenticated I throw an HttpException 401. However, the RestSharp client seems to translate this into a Http 500 status code.

Chrome popup window always on top

javascript google-chrome google-chrome-extension

I have a Chrome app that opens a popup window. I would like it to be able to stay on top, but am currently unable to do so.