Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
How to add the panels dynamically when I used in the while loop?
I know to get the panels by programming but my problem is that how to change the name for each and every time?
Please, give me an idea.
C#
while (dr.Read())
{
    i = i + 1;
    Panel a = new Panel();
}
Posted
Updated 8-Aug-10 0:51am
v2
Comments
koool.kabeer 7-Aug-10 3:57am    
if you want to change the name of Panel...

Panel p = new Panel();
p.Name="someName";

1 solution

try this..
while (dr.Read())
          {
              i = i + 1;
              Panel p = new Panel();
              p.Name="panel" + i.ToString();
              parentControl.Controls.Add(p);

          }

now to get the panels
foreach(Control c in parentControl.Controls)
{
    if(c.GetType().Equals(typeof(Panel))
    {
        Panel temp = (Panel)c;
        if(temp!=null && temp.Name=="panel3")/*'panel3' is the name which you have given while adding the panels*/
             MessageBox.Show("Panel Found");
    }
}


and if you dont want to loop the Controls of parentControl...
you can just access the particular panel by
Panel temp = (Panel)parentControl.Controls["panel3"];/*'panel3 is name which you have given while adding*/
if(temp!=null)
  MessageBox.Show("Panel Found);

you can also get the Panel Collection of the 'parentControl' by using this statement..
/*please ignore if you dont get this statement as you can be done with the above statements*/
var panelCollection = from Control c in parentControl.Controls where c.GetType().Equals(typeof(Panel)) select c;

foreach(Panel p in panelCollection)
{
   /*do what you need to do with the panels here*/
}


hope this helps you..
take care
 
Share this answer
 
v2
Comments
[no name] 7-Aug-10 4:14am    
Reason for my vote of 5
exact anwser
\
koool.kabeer 7-Aug-10 4:15am    
be happy and keep your kool.. thanx for sharing dear
Sandeep Mewara 7-Aug-10 10:41am    
Reason for my vote of 5
Wooo... nicely putup & explained!

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