Click here to Skip to main content
15,910,358 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I put a Datagridview inside a UserControl. and I load this usercontol inside a panel in my mainform by pressing a button but it dosent show any data from my database
my code is for button click:

C#
foreach (Control ctrl in panelMain.Controls)
            {
                ctrl.Dispose();
            }
           
            panelMain.Controls.Add(new usercontrol());
Posted

The code fragment you have given us may / may not display any data: it is all dependant on the usercontrol constructor you have written.

Since we can't see if you have made any database accesses in your constructor, we can't see if there is a problem.

But, at a guess, when you designed it, you probably set a datasource value in the designer? Do you do that in your constructor?
 
Share this answer
 
Comments
rad444 12-Feb-11 9:21am    
I set a datasource in the designer only
what I have to do with constructor ?
OriginalGriff 12-Feb-11 9:29am    
When you set it in the designer, you set it only for the instance you were testing.
When you add it to your panel, you create a new instance - it has the default values of the control.
Either
1) modify the constructor of the usercontrol to set the DataSource correctly (not recommended)
or
2) Set the DataSource when you create the instance.

It don't recommend the first option because it fixes the control to a particular form. Make it flexible, so you can re-use it.

BTW: Change it's name! "usercontrol" is not exactly descriptive of what it does! :laugh:
rad444 13-Feb-11 5:23am    
Can you explain about 2nd method by an example or is there any resource I refer to ?

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