Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an application, that has 2 usercontrols

In the first usercontrol, there is a datagridview and within that, there is a function that gets the data and fills the datagridview.
public void getdata()
        {
            //get data and fill datagrid view...
        }

This function is being called on usercontrol load

In the 2nd user control, after doing registration, etc. I want to call the getdata() function again so the datagridview is updated in 1st usercontrol.

What I have tried:

so this is what I tried:

Code_Sign uc = new Code_Sign();
uc.getdata();

//code_sign is name of 1st usercontrol

Doing this, the function is called (I tested this by putting a msgbox in the function. The msgbox is displayed)

However, the problem is this. The datagrid view has no changes, e.g the new data is not inserted into the datagridview. Same old data is displayed.

in the getdata() function. I have

bunifuCustomDataGrid1.Rows.Add(
                new object[]
                {
                    issuer,
                    data,
                    x509.SerialNumber,
                    x509.FriendlyName
                }
                );

(I have also tested this with normal datagridview instead of bunifudatagrid. It doesn't work)

I have set modifier of datagrid view to public but i just can't seem to have it working in any way.

Any help would be appreciate. I'm not sure what I am missing. Maybe there is another way which I am not aware of.

Thanks in advance
Posted
Updated 15-Oct-19 21:00pm

1 solution

The basic answer is: don't.
A UserControl should know about itself and the controls it contains: it shouldn't know about the form that contains it (because it might be another UserControl istead for example), and it certainly shouldn't know about other controls which may be on the parent form.
Instead, it raises an Event which the parent control / form handles, and it decides what to do about it.
In your case, the second UserControl signals a "Registered" Event, the parent form handles it, gets any relevant registration data from the the control, and passes it to the first UserControl to update itself.

See here: Transferring information between two forms, Part 3: Child to Child[^] - it deals with forms but it's exactly the same procedure and code for UserControls.
 
Share this answer
 
Comments
Member 13861271 17-Oct-19 14:45pm    
Thanks for the answer. I do like the approach you have showed me. However, I think it's more work than needed doing it this way. In my case. (in my opinion) the most simple approach would be. When someone registers, have the 2nd usercontrol signal the first user control that its time to update. (There is not really need to pass data) As if the first user control just triggers the getdata() function within it. it will all be updated. Do you think this is a good approach and if so, maybe if you have a sample example for it. I can't seem to figure that out on my own. Any help is appreiciate. Thanks
OriginalGriff 18-Oct-19 1:55am    
So, let me summarize: You have an bad idea, I explain why it's a bad idea, and show you how to do it properly, with code and and example app ready written, you want to ignore all that and implement the bad idea anyway, and you want me to write the code to implement a bad idea for you because you don't know how to make it work?
Member 13861271 22-Oct-19 12:34pm    
I literally just asked if it's a good approach or not. and if it's a good approach, then help me with it because it made more sense to me. I don't need your help if you can't be nice about it.
OriginalGriff 22-Oct-19 12:39pm    
"I do like the approach you have showed me. However, I think it's more work than needed doing ..."
What do you think that sounds like, apart from "you want to ignore all that and implement the bad idea anyway, and you want me to write the code to implement a bad idea for you because you don't know how to make it work?"

That's not me "not being nice about it", that's you saying "stuff you" to me ...
Member 13861271 22-Oct-19 14:08pm    
"Do you think this is a good approach" you could have literally said 'no this approach is not good because <reason> and you should do what I mentioned above' I am still learning C# and I just wanted to discuss it with someone who knows more and learn from it. Guess not. It would have been a much-appreciated answer instead of what you actually answered me with.

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