views:

51

answers:

3

I have a datagridview that I would like all values on a specific row passed to a new form. I can extract the values, but how would you recommend passing these into the new form (each will be assigned to a specific textbox)?

+1  A: 

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.

smoore
+1: Agreed, just keep in mind that if passing the DataGridViewRow, the entire DataGridView is given to the new form.
galford13x
I'm not sure I understand what you mean? Do you mean if I simply pass in dataGridView.Rows[i] as an argument, instead of assigning it to a new variable? I could see that passing in a reference to the row, but not the whole grid.
smoore
If you do new MyForm(dataGridView.Rows[0]); the new form will have the DataGridViewRow. It can then access the entire DataGridView through the DataGridViewRow.DataGridView property. Assigning it to a new variable as I showed above does that same thing.
galford13x
I should clarity that passing the DataGridViewRow doesn't explicitly pass the DataGridView. It only exposes the DataGridView.
galford13x
I see what you're saying. My advice was meant to be the same as what you gave (pass in a copy of the DataGridViewRow). I guess I didn't express that very well though. Thanks for pointing that out to me.
smoore
A: 

Most controls are based on the Control base class. That class has the Tag property which can hold user data. Attach the data to the row or cell as needed. I discuss on my blog entitled: C# Winforms and the Hidden Association Tag. HTH

OmegaMan
+4  A: 

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.

galford13x
@smoore: Indeed, I shall update.
galford13x
+1: Good answer and comment withdrawn.
smoore
+1 I set it up as an Array list at first but wasnt sure if that was the optimal solution. Thanks for your help.
Farstucker