Click here to Skip to main content
15,906,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm kinda stuck and not sure how I should go about this.

I have a form where I collect some information, and the user clicks a button to save the info to a datagridview control. The problem is that the datagridview is in another form.

I initially tried to pass the information using the following code, but it won't work because the target method needs to be static, and I can't make it static (so far as I know) because that method calls another method on an object, and a static method cannot reference an object.

C#
\
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            string[] ingredient = {this.AddIngredient_Quantity.Text + " "
                                   this.AddIngredient_Unit.Text,
                                   this.AddIngredient_Ingredient.Text,
                                   this.AddIngredient_Format.Text };
            EditRecipe.addIngredient(ingredient);
        }


This code is in a form called AddIngredient.cs, and as you can see, the information is to be added to EditRecipe.cs. The following is what I have so far for code:

C#
public static void addIngredient(string[] ingredient)
{
    ingredients.Rows.Add(ingredient);
}


This cannot work because EditRecipe.ingredients is an instance of DataGridView.

How can I possibly hack this to accomplish my goal?
Posted

1 solution

This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

—SA
 
Share this answer
 
Comments
agent154 23-Dec-11 20:28pm    
I did a few CS courses a couple years back, and Interfaces were one of very few subjects that I could just not understand for the life of me. I googled it now and I think I have a bit better of an understanding, but I still have no idea how I would go about implementing such functionality between my two forms.
agent154 23-Dec-11 20:49pm    
I think part of my confusion here is because Form1 calls Form2, and in Form2, changes to data are made. When hitting the save button, I need that data to be passed back to Form1. All of these examples seem to imply that the form where the data is going is being called directly. Form2 does not call Form1, it only acts as a dialog box with a save button.
agent154 23-Dec-11 21:33pm    
After reading up a bit more, I still have no idea why interfaces are even necessary; they seem to offer no additional functionality, as far as I can understand.

In any event, I managed to solve the problem by passing a reference to Form1 to Form2 via 'AddIngredient form2 = new AddIngredient(this)' and then used the reference in Form2 to invoke the 'addIngredient()' method. It works as expected. Thanks anyhow.
Sergey Alexandrovich Kryukov 24-Dec-11 0:47am    
I explain below why interfaces are better. Yes, they do not add any functionality, but this is not the only goal. They improve discipline which ultimately makes code more maintainable and less error-prone.
--SA
agent154 24-Dec-11 0:31am    
OK, I think I have to take that back... after a ton of experimenting with inheriting, I found out that interfaces work almost exactly like inheriting a whole class, except on a smaller scale. I understand how things work now!

Thanks for the tip.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900